iPhone-如何确定自定义UITableViewCell中的哪个按钮被按下 [英] iPhone - How to determine in which cell a button was pressed in a custom UITableViewCell

查看:58
本文介绍了iPhone-如何确定自定义UITableViewCell中的哪个按钮被按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个UITableView,其中装有一个位于单独笔尖中的自定义UITableViewCell.在单元格中,有两个按钮连接到自定义单元格类中的操作.当我单击一个按钮时,可以调用适当的方法,但是我需要知道按钮被按下的那一行.每个按钮的tag属性都为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中的哪个按钮被按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆