【问题标题】:Determine UIButton in UITableViewCell确定 UITableViewCell 中的 UIButton
【发布时间】:2013-04-28 06:48:40
【问题描述】:

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

按钮添加在 UITableViewCell 的自定义类中(而不是在 cellForRowAtIndexPath 中)。

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

需要帮助!!!谢谢

【问题讨论】:

    标签: iphone uitableview uibutton uigesturerecognizer


    【解决方案1】:

    试试这个

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

    或者在cellForRowAtIndexPath方法中给每个UIButton打标签

    并获取单元格对象

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

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多