我有一个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");
}
答案 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);
}