表格单元格中的 UISwitch -- 确定选定的 indexPath [英] UISwitch in Table Cell -- determine selected indexPath

查看:34
本文介绍了表格单元格中的 UISwitch -- 确定选定的 indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewCell 的附件视图中使用了 UISwitch.

I'm using a UISwitch in the accessoryView of a UITableViewCell.

开关在选择或取消选择时使用目标操作进行通信:

The switch uses target-action to communicate when it has been selected or deselected:

switchV.addTarget(self, action: "onChangeSwitch:", forControlEvents: UIControlEvents.ValueChanged)

问题在于找出哪个开关被选中.我知道3种方法.不知何故,每一个都不令人满意.

The problem is figuring out which switch was selected. I know of 3 methods. Each is unsatisfying somewhow.

1.我可以使用标签:

switchV.tag = indexPath.row

但是如果你有部分(我有),这很糟糕,因为我需要将它解析为两个数字的部分/行格式.

But this sucks if you have sections (and I do), because I need to parse it into and out of a two-number section/row format.

2.我可以使用数据模型并将切换视图存储在单元格正在绘制的数据项上:

2. I could use the data model and store the switch view on the data item that the cell is drawing:

dataItem.switch.addTarget(self, action: "onChangeSwitch:", forControlEvents: UIControlEvents.ValueChanged)
cell.accessoryView = dataItem.switch

然后,我可以通过遍历我的数据集并与发件人进行身份匹配来识别选择了哪个交换机.这是很多循环,我不想将视图放入我的数据模型中.

I can then identify which switch was selected by looping over my data set and doing an identity match with sender. This is a lot of looping, and I don't want to put views in my data model.

3. 然后是这个方法,它使用开关的坐标来找到排.无状态,不涉及字符串解析或数据模型混淆,但坐标,rly?我可以相信吗?

3. Then there's this method that uses the coordinate of the switch to find the row. Stateless-ish, involves no string parsing or data model muddying, but coordinates, rly? Can I trust it?

tableView.indexPathForRowAtPoint(
  sender.convertPoint(CGPointZero, toView: tableView)
)

有没有更好的方法获取被选中的UISwitch的indexPath?

Is there a better way to get the indexPath of the UISwitch that is selected?

推荐答案

我认为方法 3 非常好.但是,如果您希望它非常干净,这就是我会做的.

Method #3 is pretty good in my opinion. However if you want it to be really clean here's what I would do .

  • 声明一个自定义 TableviewCell 说 CustomTableViewCell 并有一个称为 CustomCellDelegate 的委托协议.在这里,代表会得到这样的通知:

  • Declare a custom TableviewCell say CustomTableViewCell and have a delegate protocol called CustomCellDelegate. Here the delegate gets informed like so :

 -(void)switchChanged:(UISwitch*)switch inCell:(CustomTableViewCell*)cell

  • 在 cellForRowAtIndexPath 中将您的视图控制器设置为单元格的代表.

  • In cellForRowAtIndexPath set your view controller as the delegate of the cell.

    将开关添加到您的自定义单元格并将该单元格作为目标并实现开关的操作方法.在 action 方法中调用委托:

    Add the switch to to your custom cell and make the cell as the target and implement the switch's action method. Inside the action method call the delegate :

    -(void)switchChanged:(id)sender {
        if(self.delegate && [self.delegate respondsToSelector:@selector(switchChanged:inCell:])) {
            [self.delegate switchChanged:sender inCell:self];
    }
    

  • 现在在您的 viewController 中使用委托方法中传入的单元格来计算索引路径:

  • Now in your viewController use the passed in cell in the delegate method to calculate the index path :

     -(void)switchChanged:(id)sender inCell:(CustomTableViewCell*)cell {
        NSIndexPath *path = [self.tableview indexPathForCell:cell];
     }
    

  • 它有点工作,但如果你想以正确的方式去做,你可以.

    Its a bit of work but if you want to do it the proper way , you can .

    这篇关于表格单元格中的 UISwitch -- 确定选定的 indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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