表格视图重新加载部分崩溃

jco*_*_82 8 objective-c reload uitableview ios sections

我有一个包含4个部分的表视图,每个部分有1-2个表视图单元格.第一个单元格具有uiswitch作为附件视图,并控制应用程序的颜色主题,在白天模式和夜晚模式之间切换.一旦按下开关,就会调用一个函数,改变导航栏的颜色和背景颜色.在那个功能中我也放了这条线

[self.tableview reloadData];
Run Code Online (Sandbox Code Playgroud)

使用新颜色更新表本身.它工作正常,但没有动画,所以我用它来代替

[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)] withRowAnimation:UITableViewRowAnimationFade];
Run Code Online (Sandbox Code Playgroud)

当使用该行时,开关卡住并且应用程序冻结.它不会崩溃,即没有崩溃日志,它只是冻结,uiswitch停止动画中期.

我注意到我可以重新加载没有包含附件视图的单元格的部分,并且它与淡入淡出动画完美配合.即这是有效的

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade];
Run Code Online (Sandbox Code Playgroud)

因为第三部分没有任何带附件视图的单元格.但任何包含附件视图的单元格(即第0和第2部分)的部分,如果我尝试重新加载应用程序冻结.

任何想法为什么会这样?下面是我的cellForRowAtIndexPath

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

static NSString *cellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

if (indexPath.section == 0) {

    cell.textLabel.text = [section0 objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row == 0) {

        cell.accessoryView = colorSchemeSwitch;
    }

    else if (indexPath.row == 1) {

        cell.accessoryView = autoLockSwitch;
    }
}

else if (indexPath.section == 1) {

    cell.textLabel.text = [section1 objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

else if (indexPath.section == 2) {

    cell.textLabel.text = [section2 objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%i \t", (int)app.fontSize];
    fontSizeSlider.value = app.fontSize;
    cell.accessoryView = fontSizeSlider;
}

else if (indexPath.section == 3) {

    cell.textLabel.text = [section3 objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.text = app.color;
}

cell.backgroundColor = app.cellBackgroundColor;
cell.textLabel.textColor = app.textColor;
UIView *backgroundColorView = [[UIView alloc] init];
backgroundColorView.backgroundColor = app.cellSelectedColor;
[cell setSelectedBackgroundView:backgroundColorView];

return cell;
}
Run Code Online (Sandbox Code Playgroud)

pju*_*nas 8

我在同样的问题UITableViewCell包括UISwitch.我[self.tableview reloadData]在重新加载部分之前通过调用解决了这个问题:

NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:2];
[self.tableView reloadData];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
Run Code Online (Sandbox Code Playgroud)

如果您致电同样的问题:

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade]
Run Code Online (Sandbox Code Playgroud)

无需先重新加载数据.

  • 如果我们调用reload表,那么动画需要重新加载部分?(H) (16认同)
  • 我同意@dip,应该调用"reloadData"或"reloadSections",但不能同时调用.话虽如此,我在"beingUpdates"和"endUpdates"之间只使用"reloadSections"随机获得SIGABRT崩溃.我还在试图找出原因...... (5认同)
  • 我同意上述评论.使用reloadData或reloadSections,两者都没有意义 (5认同)

Fra*_*ain 5

您还没有添加崩溃日志,但我猜您收到一个错误,指出某个部分中的现有行数必须等于更新之前的某个值。如果是这样的话,你[self.tableview beginUpdates]在重新加载表之前和[self.tableview endUpdates]重新加载之后使用过吗?