Tai*_*red 20 iphone uibutton uitableview
我目前有一个UITableView,其中填充了一个单独的笔尖中的自定义UITableViewCell.在单元格中,有两个按钮连接到自定义单元类中的操作.当我单击其中一个按钮时,我可以调用正确的方法,但我需要知道按下哪一行按钮.每个按钮的标签属性为0.我不需要知道整个单元格的时间选择,只是当按下一个特定的按钮时,所以我需要知道它是哪一行,所以我可以更新正确的对象.
JOM*_*JOM 21
更简单的解决方案是使用(id)发送器定义您的按钮回调,并使用它来挖掘表行索引.这是一些示例代码:
- (IBAction)buttonWasPressed:(id)sender
{
NSIndexPath *indexPath =
[self.myTableView
indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSUInteger row = indexPath.row;
// Do something with row index
}
Run Code Online (Sandbox Code Playgroud)
很可能你使用相同的行索引来创建/填充单元格,因此识别按钮现在应该做什么应该是微不足道的.无需使用标签并尝试将它们整理好!
澄清:如果你目前使用- (IBAction)buttonWasPressed; 只需将其重新定义为- (IBAction)buttonWasPressed:(id)sender; 额外的回调参数就在那里,不需要做任何额外的事情来获得它.还记得在Interface Builder中将按钮重新连接到新的回调!
对于不依赖于标记或视图层次结构的实现,请执行以下操作
- (void)btnPressed:(id)sender event:(id)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9759 次 |
最近记录: |