UITableCell按钮touchUpInside单击了单元格索引按钮

时间:2014-01-17 20:44:45

标签: iphone objective-c

我有一个UITable与UITableCells,如下所示:

[myLabel myButton]

[myLabel myButton]

[myLabel myButton]

[myLabel myButton]

我希望我的handlerCellButtonClicked也能获得对单元格索引的引用。当触摸按钮时,如何获得对该索引的引用?

/** called by table to get the cell needed by the index */
-(UITableViewCell*)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    //init cell
    ...
    //configure the cells custom button handlers
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

/** handler when the button in the cell is clicked */
- (void)handlerCellButtonClicked{
    NSLog(@"cell myButton clicked");
}

1 个答案:

答案 0 :(得分:2)

使用UIButton Tag属性。

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

    //init cell
    //configure the cells custom button handlers

    cell.myButton.tag = indexPath.row;
    [cell.myButton addTarget:self action:@selector(handlerCellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

/** handler when the button in the cell is clicked */
- (void)handlerCellButtonClicked:(UIButton *)sender
  {
     NSLog(@"%d cell myButton is clicked", sender.tag);
  }