0

我是新手。

我有一个包含 3 个项目的自定义 UITableViewCell:

一个 UILabel 一个 UITextField 和一个 UIButton

我的问题是,我怎么知道从哪个单元格单击了按钮或编辑了 textField ?

有没有办法追踪它们?

感谢您的任何回复。

4

2 回答 2

1

我通常做的是在配置自定义单元格后,将单元格的 NSIndexPath 保存在状态 UIControlStateApplication 的按钮标题中。

在您的 tableView: cellForRowAtIndexPath: 方法中,配置单元格时:

[theButton setTitle:(NSString *)indexPath forState:UIControlStateApplication];

然后,在您的按钮操作中:


- (IBAction) buttonWasClicked:(UIButton *) senderButton {
    NSIndexPath *indexPath;

    indexPath = (NSIndexPath *)[senderButton titleForState:UIControlStateApplication];

    NSLog(@"Sender button was clicked in cell at NSIndexPath section: %d row: %d ", [indexPath section], [indexPath row]);
}

我对这种方式并不满意。如果有人有更清洁的方法,我很想听听。

于 2010-02-15T10:41:55.470 回答
1

如果您只有一个部分,则可以将其存储在indexPath.rowbutton.tag并在该按钮触发的操作中检索它。

于 2010-02-15T11:03:12.043 回答