在UITableView中检测从哪个单元格,UIButton被按下

时间:2015-05-20 13:01:38

标签: ios uitableview

我有两个自定义UItableViewCellUIButton相同,如何确定从哪个单元UIButton调用?

P.S。我不想使用两种不同的方法。

  

cellForRowAtIndexPath方法。

if (indexPath.row==1) {
static NSString *cellIdentifier = @“CustomeCell1“;

                            NewsFeedCell1 *cell = (NewsFeedCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}
else
{
static NSString *cellIdentifier = @"CustomeCell2”;

                            NewsFeedCell2 *cell = (NewsFeedCell2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.btnPrivacy.tag = indexPath.row;
[cell.btnPrivacy addTarget:self action:@selector(btnPrivacyClicked:) forControlEvents:UIControlEventTouchUpInside];

}
  

按钮点击方法。

-(IBAction)btnPrivacyClicked:(UIButton *)sender
{
    NSLog(@"Privacy Clicked at : %ld",(long)sender.tag);

    // Here Determine From Which Cell UiButton Is Clicked.
    NewsFeedCell1 *cell = (NewsFeedCell1 *)[self.HomeTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];

2 个答案:

答案 0 :(得分:1)

// *** Use Following code to detect `Cell` ***
CGPoint buttonPosition = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
[self.tableView cellForRowAtIndexPath:indexPath];

答案 1 :(得分:0)

为cell.btnPrivacy.tag = indexPath.row;

设置标记

并在按钮操作中,您将获得按钮btnPrivacy标签,即sender.tag,它是单元格索引。

试试这个。