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

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

问题描述

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

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您的自定义单元格添加属性,可帮助您识别单元格所代表的信息。索引路径,数据模型的索引等...

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天全站免登陆