自定义 UITableViewCell 项问题

发布于 2024-07-27 01:37:20 字数 189 浏览 2 评论 0原文

我是新手。

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

UILabel 一个 UITextField 和 UIButton

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

有办法追踪他们吗?

感谢您的回复。

I am a newbie.

I have a custom UITableViewCell with 3 items:

a UILabel
a UITextField
and a UIButton

My question is, how do I know from which cell the button was clicked or textField was edited ?

Is there a way to track them?

Thanks for any replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-08-03 01:37:21

我通常做的是配置自定义单元格后,将单元格的 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]);
}

我对这种方式不太满意。 如果有人有更干净的方法来做到这一点,我很想听听。

What I usually do is after configuring the custom cell, I save the NSIndexPath of the cell in the title of the button for the state UIControlStateApplication.

In your tableView: cellForRowAtIndexPath: method, when configuring the cell:

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

Then, in your button action:


- (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]);
}

I am not really happy with this way. If someone has a cleaner way to do it, I would love to hear about it.

岁吢 2024-08-03 01:37:21

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

If you only have one section you can store the indexPath.row in the button.tag and retrieve it in the action that is triggered by that button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文