UITutton在UITableViewController中 [英] UIButton inside UITableViewController

查看:114
本文介绍了UITutton在UITableViewController中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个表格单元格中创建了一个 UIButton 。我想像这样挂钩一个触摸事件:

I have a UIButton that is created inside of each table cell. I want to hook up a touch event like so:

[imageButton addTarget:self
                action:@selector(startVote:)
      forControlEvents:UIControlEventTouchUpInside];

我想将有关当前行的数据(该行的对象的id)传递给startVote方法。是否有一种我缺少这样做的方法,或者我是否打破了一些最佳实践。这似乎是一件很正常的事情吗?

I want to pass data about the current row (the id of the object for that row) to the startVote method. Is there a method that I am missing to do this or am I breaking some best practice. This seems like a very normal thing to do?

推荐答案

我假设你有某种 NSArray 将数据传递给 cellForRowAtIndexPath 中的按钮。

I assume you have some sort of NSArray with the data that gets passed on to the buttons in cellForRowAtIndexPath.

试试这个 startVote

- (void)startVote:(id)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSDictionary *myData = [myArray objectAtIndex:indexPath.row];
}

编辑:

如果由于某种原因未选择该行,您可以在创建时为每个按钮分配唯一的标记,然后:

If for some reason the row is not selected, you can assign a unique tag to every button upon creation and then:

- (void)startVote:(id)sender {
    int myTag = [(UIButton *)sender tag];
    NSDictionary *myData = [myArray objectAtIndex:myTag];
}

也许你会用进行某种操作标记因此它可以用作索引(我为每个标记添加一定数量,因此它不会与使用的自动标记冲突操作系统。

Maybe you would do some sort of operation with the tag so it can be used as an index (I add a certain amount to every tag so it will not conflict with "automatic" tagging used by the OS.

这篇关于UITutton在UITableViewController中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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