0

可能重复:
如何知道 UITableview 行号

我在这个 tableView 上有一个 tableView 和一个 tableViewCell。我为 tableViewCell 定义了另一个名为 CustomCell 的类,以便我编写必要的自定义代码并创建一个按钮(在此单元格上)。单击 tableViewCell 上的按钮时,我想了解哪个 tableViewCell 包含该按钮,以便我可以仅对该单元格(包含单击的按钮)进行必要的更改

我如何理解哪个 tableViewCell 包含被点击的按钮?

4

6 回答 6

0

如果您将附件详细信息按钮替换为自定义按钮,则可以调用附件按钮TappedForRowWithIndexPath: 方法。

示例 - 将其放在 cellForRowAtIndexPath 中(设置单元格时):

UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 24)];
[myAccessoryButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
[myAccessoryButton release];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

然后作为一个单独的方法:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:    (NSIndexPath *)indexPath
{
    //do something
}
于 2013-01-23T18:27:26.063 回答
0

实现这一点的一种策略是将带有行号的标签分配给按下的按钮:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexpath {
    YourCustomCell *cell = // Code
    cell.button.tag = indexPath.row;

    // Other cell preparation code

    return cell;
}

然后,在按钮的动作中,你可以看到它的标签是什么,以确定对应的模型对象是什么:

- (void)didSelectButton:(id)sender {
    UIButton *button = sender;
    NSInteger tag = button.tag;

    YourModelObject *item = self.items[tag];

    //    Continue with the business logic for what should
    //    happen when that button is pressed.
}
于 2013-01-23T18:27:55.883 回答
0

这个怎么样...

- (UITableViewCell *)cellWithSubview:(UIView *)subview {
    while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview;
    return (UITableViewCell *)subview;
}

- (IBAction)buttonClicked:(id)sender {
    UITableViewCell *theCell = [self cellWithSubview:sender];
}
于 2013-01-23T18:29:46.970 回答
0

您需要以这种方式调用此cellForRowAtIndexPath方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10 , 10, 200, 20);
    [button addTarget:self action:@selector(passRowValueMethod:) forControlEvents:UIControlEventTouchUpInside];
    [button setTag:indexPath.row];
    [button setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
    [cell addSubview:button];


    return cell;
}

然后定义按钮目标方法并在该方法中获取标签值。

-(void)passRowValueMethod:(UIButton*)sender
{
    UIButton *buttonGet = sender;
    NSLog(@"%d", buttonGet.tag);
}
于 2013-01-23T20:21:40.040 回答
0

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect frame = CGRectMake(0.0, 10.0, 24, 24);
button.frame = frame;
[button setTag:((indexPath.section & 0xFFFF) << 16) |
 (indexPath.row & 0xFFFF)];
[button setImage:[UIImage imageNamed:@"link-buttoni4.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"link-button-onclicki4.png"] forState:UIControlStateHighlighted];
[button setSelected:NO];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

}

-(void)checkButtonTapped:(UIButton *)sender {

NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
NSUInteger row     = (sender.tag & 0xFFFF);
NSLog(@"Button in section %i on row %i was pressed.", section, row);

}

于 2013-01-24T05:19:18.550 回答
0

试试这个,
你可以知道在这个方法中单击的单元格的部分和行号:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}

1>section 可以通过
int sectionno = indexPath.section
2>cell 索引可以通过
int rowno = indexPath.row
获得然后使用你可以像这样得到表格单元格,

UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowno inSection:sectionno]];
于 2013-01-24T10:56:56.243 回答