2014-01-17 23 views
0

我有UITableCells一个UITable其看起来如下:与卫生组织纽扣电池的索引UITableCell按钮touchUpInside被点击

[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

[其中的UIButton在一个UITableView按压检测]的可能重复(http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) –

回答

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); 
    } 
+0

如何你确定选择者会通过发件人吗? – hgwhittle

+0

(handlerCellButtonClicked :)应该将发送者传递给目标,对吧? –

+0

我不确定。我很想知道它是否有效。 – hgwhittle