我有一个包含4个部分的表格视图,每个部分有1-2个表格视图单元格。第一个单元具有uiswitch作为附件视图,并控制应用程序的颜色主题,在白天模式和夜间模式之间切换。按下开关后,将调用一个函数,以更改导航栏的颜色和背景颜色。在该函数中,我也将以下行放在一行中
[self.tableview reloadData];
用新颜色更新表格本身。它工作正常,但是没有动画,所以我改用了它
[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)] withRowAnimation:UITableViewRowAnimationFade];
使用该行时,开关卡住,应用程序冻结。它不会崩溃,也就是说,没有崩溃日志,它只是冻结而uiswitch会在动画中停止。
[我注意到,我可以重新加载没有包含附件视图的单元格的节,并且它与渐变动画完美配合。即这有效
[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;
}
[包含UITableViewCell
的UISwitch
中有相同的问题。我通过在重新加载各节之前调用[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]
?
尝试重新加载特定行时遇到相同的问题。我的dataSource不一致。因为我正在从远程加载数据。我发现此link解决了问题。但是我需要修改这一行。
let sections = dataSource.numberOfSections?(in: self) ?? 0
收件人
let sections = dataSource.numberOfSections?(in: self) ?? 1