表视图重新加载部分崩溃

我有一个表格视图,每节有4个部分和1-2个表格视图单元格。 第一个单元有一个uiswitch作为附件视图,并控制应用程序的颜色主题,在白天模式和夜间模式之间切换。 一旦开关被击中,一个函数被调用,改变导航栏的颜色和背景颜色。 在那个函数中,我也放了一行

[self.tableview reloadData]; 

用新颜色更新表格本身。 它工作正常,但没有animation,所以我用这个来代替

 [self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)] withRowAnimation:UITableViewRowAnimationFade]; 

当使用该行时,开关卡住,应用程序冻结。 它并没有崩溃,即没有崩溃日志,它只是冻结,并且uiswitch在animation中间停止。

我注意到,我可以重新加载没有包含附件视图的单元格的部分,并且与淡入淡出的animation完美配合。 即这工作

 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; 

因为第三部分没有附件视图的单元格。 但是,如果我尝试重新加载应用程序,任何包含附件视图的单元格(即,区段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; } 

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

 NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:2]; [self.tableView reloadData]; [self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade]; 

如果你打电话,也会发生同样的问题

 [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade] 

而不用重新加载数据。

你还没有添加一个崩溃日志,但即时通讯猜测你得到一个错误,指出一个节中的现有行数必须等于更新前的某个值。 如果是这样的话,在重装表之前是否使用了[self.tableview beginUpdates] ,重载之后是否使用了[self.tableview endUpdates]