如何在基于节的表视图中获取开关更改事件的单元格索引路径 [英] How can I get index path of cell on switch change event in section based table view

查看:18
本文介绍了如何在基于节的表视图中获取开关更改事件的单元格索引路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义单元格,右侧有几个单元格的开关.我想要的是在开关更改事件中存储特定单元格的值.表视图有数字部分,所以我不能为 switch 设置标签,因为我需要部分和行来获取索引路径.

I have custom cell having switch in few cells at right side. what I want is to store value of specific cell on switch change event. Table view has number sections so I can't set tag for switch because I need section as well as row to obtain index path.

任何建议,任何替代方案,但我必须在基于部分的表格视图中使用 UISwitch.

Any suggestion any alternative but I have to use UISwitch in section based table view.

谢谢

推荐答案

在您的自定义单元格中添加帮助您识别单元格所代表信息的属性.索引路径、数据模型的索引等...

In your custom cell add properties which help you identify the information the cell represents. Index path, indexes for your data model etc...

然后向单元格添加一个块属性,当单元格开关更改时,您可以调用它来告诉 UITableView 或任何其他代码段.例如

Then add a block property to the cell which you can call to tell a UITableView or any other piece of code when a cell switch changes. e.g.

@property (nonatomic,copy) void (^onSwitchChange)(UITableViewCell *cell);

在您的自定义单元格代码中,为 UISwitch 添加一个动作处理程序.当它触发时,调用 self.onSwitchChange(self) 这将通知注册 onSwitchChange 块的代码开关已更改以及在哪个单元格上.

Inside your custom cell code, add an action handler for the UISwitch. When it fires, call self.onSwitchChange(self) which will notify the code which registered an onSwitchChange block that a switch has changed and on which cell.

在创建单元格时的表格视图中,按如下方式设置 onSwitchChange 块:

In your table view when you create the cell, set the onSwitchChange block as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
  <snip>

  YourUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:yourCellIdentifier forIndexPath:indexPath];
  cell.onSwitchChange=^(UITableViewCell *cellAffected){
    // Add code to deal with the swicth switch using properties of cellAffected
    ... Your handler code here ...
  }];

  <snip>
}

这使您可以处理表视图控制器中的所有更改.希望这会有所帮助.

This lets you handle all the changes in the table view controller. Hope this helps.

这篇关于如何在基于节的表视图中获取开关更改事件的单元格索引路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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