iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell(iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格)
问题描述
我目前有一个 UITableView,其中填充了一个单独的 nib 中的自定义 UITableViewCell.在单元格中,有两个按钮连接到自定义单元格类中的操作.当我单击其中一个按钮时,我可以调用正确的方法,但我需要知道按钮被按下的是哪一行.每个按钮的标记属性为 0.我不需要知道整个单元格的时间被选中,只是在按下特定按钮时,所以我需要知道它是哪一行,这样我才能更新正确的对象.
I currently have a UITableView that is populated with a custom UITableViewCell that is in a separate nib. In the cell, there are two buttons that are wired to actions in the custom cell class. When I click one of the buttons, I can call the proper method, but I need to know which row the button was pressed in. The tag property for each button is coming as 0. I don't need to know when the entire cell is selected, just when a particular button is pressed, so I need to know which row it is so I can update the proper object.
推荐答案
更简单的解决方案是使用 (id)sender 定义按钮回调 并使用它来挖掘表格行索引.下面是一些示例代码:
Much easier solution is to define your button callback with (id)sender and use that to dig out the table row index. Here's some sample code:
- (IBAction)buttonWasPressed:(id)sender
{
NSIndexPath *indexPath =
[self.myTableView
indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
// Do something with row index
}
您很可能使用相同的行索引来创建/填充单元格,因此确定您的按钮现在应该做什么应该是微不足道的.无需玩标签并尽量保持它们井井有条!
Most likely you used that same row index to create/fill the cell, so it should be trivial to identify what your button should now do. No need to play with tags and try to keep them in order!
澄清:如果您当前使用-(IBAction)buttonWasPressed;只需将其重新定义为-(IBAction)buttonWasPressed:(id)sender; 额外的回调参数就在那里,不需要做任何额外的事情来获取它.还记得将您的按钮重新连接到 Interface Builder 中的新回调!
Clarification: if you currently use -(IBAction)buttonWasPressed; just redefine it as -(IBAction)buttonWasPressed:(id)sender; The additional callback argument is there, no need to do anything extra to get it. Also remember to reconnect your button to new callback in Interface Builder!
这篇关于iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iPhone - 如何确定在自定义 UITableViewCell 中按下按钮的单元格
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01