【问题标题】:UISwitch chang status after reusing the cell重用单元格后 UISwitch 更改状态
【发布时间】:2013-12-25 09:51:39
【问题描述】:

您好,我在原型单元格上有一个UISwitch

当我将UISwitch 更改为OFF 并通过滚动将单元格移出UITableView 框架然后向后滚动时-UISwitchON

但是进行了更改 - 因为当我再次调用 UITableView 控制器以显示在屏幕上时 - UISwitchOFF

代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingsOptionCell * cell = [tableView dequeueReusableCellWithIdentifier: @"SettingsOptionCell"];
    NSDictionary *data = [NSDictionary new];
    cell.onOption.hidden = NO;

    switch (indexPath.section) {

        case SettingsControllerServiceSection:
            data = self.serviceAttr[indexPath.row];
            break;

        case SettingsControllerStatusSection:
            data = self.statusAttr[indexPath.row];
            break;

        default:
            break;
    }

    cell.captionLabel.text = data[@"name"];
    cell.onOption.on = [data[@"check"] boolValue];
    cell.optionId = data[@"id"];
    cell.updateHandler = ^(){
        if (self.updateHandler) {
            [self update];
            self.updateHandler();
        }
    };
    return cell;
}

【问题讨论】:

  • 可能与您的updateHandler有关?
  • 请分享SettingsOptionCell类的代码
  • 在更改开关时将字典中的数据值检查设置为 ON 或 OF。发生这种情况是因为字典中的检查数据相同,最好你做一件事尝试在 Swith 时使用 BOOL 值在 dictinoray 中或在 dictinoray 中,那么我认为您的问题已解决,必须使数据数组全局化

标签: ios objective-c uitableview uiswitch


【解决方案1】:

非常有趣的代码。 起初:我看不到您尝试在哪里创建单元格(dequeueReusableCellWithIdentifier:仅从可重用队列中选择对象,我看不到您将其放在哪里)。 第二次:

cell.onOption.on = [data[@"check"] boolValue];

每次应该将单元格放置在 tableView 中时,您都可以从字典中设置 switch 的值。你确定你改变了这个值吗?

附:每次单元格可见时(您的滚动表)都会被调用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

【讨论】:

  • 差点忘了! NSDictionary *data = [NSDictionary 新];真的吗?为什么你需要 switch than?在目标 C nil 中效果很好。将消息发送到 nil 或空字典(我的意思是结果)没有区别。
猜你喜欢
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2019-08-26
  • 1970-01-01
  • 2020-02-29
  • 2013-04-19
  • 2021-04-05
  • 2011-04-08
相关资源
最近更新 更多