确定UITableViewCell中的UIButton

时间:2013-04-28 06:48:41

标签: iphone uitableview uibutton uigesturerecognizer

我在每个UITableViewCell中都有UIButton。如何检查按下按钮的单元格?

在UITableViewCell的自定义类中添加了Button(不在cellForRowAtIndexPath中)。

工作原理:我在每个UITableViewCell(分组Tableview)中都有thumbUp,我需要知道哪个单元是按下thumbUp的。我一次可以看到最多2个单元格(因为单元格高度为400)。每次调用cellForRowAtIndexPath时我都尝试使用递增计数器。它适用于第一个单元格,但是当我向下滚动并且第二个单元格调用cellForRow时,计数器被抬起,但是第一个单元格的thumbUp按钮仍然可见,如果我按下该thumbUp,它将像第二个单元格thumbUp一样被按下。希望你明白。

需要帮助!!!感谢

1 个答案:

答案 0 :(得分:2)

试试这个

- (IBAction)buttonWasPressed:(id)sender
{
    NSIndexPath *indexPath =
        [self.myTableView
         indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;

    // Do something with row index
}

或为UIButton方法

中的每个cellForRowAtIndexPath添加标记

获取单元格对象

 - (IBAction)buttonWasPressed:(id)sender
    {
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:sender.tag];
    }
相关问题