将参数传递给选择器 [英] Pass an argument to selector

查看:128
本文介绍了将参数传递给选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIButton在我的自定义单元格,我想触发一个动作,当用户按下该按钮,但我需要知道从哪一行按下UIButton。

I have a UIButton inside my custom cell and I want to trigger an action when the user presses that button BUT I need to know from which row the UIButton was pressed.

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;

    ...

    [button addTarget:self action:@selector(buttonPressedAction:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonPressedAction:(UIButton *)button
{
    //int row = indexPath.row;
    //NSLog(@"ROW: %i",row);
}

如何将indexPath作为参数传递给我的选择器?

How do I pass the indexPath as argument to my selector?

推荐答案

您可以将按钮的标签属性设置为行号:

You can either set the tag property of the button to the row number:

button.tag = indexPath.row;

并使用

NSInteger row = button.tag;

或将索引路径对象本身设置为按钮的关联对象:

or set the index path object itself as an associated object to the button:

objc_setAssociatedObject(button, "IndexPath", indexPath);

NSIndexPath *ip = objc_getAssociatedObject(button, "IndexPath");

这篇关于将参数传递给选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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