当我单击“切换"时,如何获取indexPath.row/path? [英] How to get indexPath.row/path When I click the Switch in a row?

查看:73
本文介绍了当我单击“切换"时,如何获取indexPath.row/path?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个或更多自定义单元格

I create tow or more custom cell

每个单元格内部都有一个开关

each cell has a switch inside

我怎么知道我单击行中的哪个开关

How can I know which switch in the row I click

例如,我单击第3行中的开关,它将返回indexPath.row = 3

ex.I click the switch in row 3,than It will return indexPath.row = 3

并且开关状态为开还是关?

and also the switch status is on or off ?

我应该输入哪个空白?

我知道有一种方法可以使indexpath返回:

I know there is a way can get indexpath return by:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

但是我不想碰那行,只需开关...

But I don't want to touch the row,just the switch ...

哦,我做了一些研究,但有一点不同

Oh I do some research but it just be a little different

如何跟踪表格视图单元格中按钮的索引路径?

有什么想法吗?

我希望我可以发布我的屏幕截图,但我不是新用户 系统不允许我这样做

I wish I can post my screen shot,but not I am a new user the system doesn't allow me to do this

推荐答案

创建开关时,为其设置目标和操作:

When you create the switch, set up a target and action for it:

[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];

在这里,目标self是您的UITableViewController,在其中您需要实现方法(void) switchToggled:(id)sender来处理控制事件.切换后,该开关将自动将自己作为sender发送给switchToggled方法.像这样(从您链接的UIButton示例中修改):

Here, the target self is your UITableViewController, in which you'll need to implement the method (void) switchToggled:(id)sender to handle the control event. The switch will automatically send itself to the switchToggled method as the sender when it is toggled. Like this (modified from the UIButton example you linked):

- (void)switchToggled:(id)sender {
    UISwitch *theSwitch = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];

    if(theSwitch.on) {
      // switch turned on
    }
    else {
      // switch turned off
    }
}

这篇关于当我单击“切换"时,如何获取indexPath.row/path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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