【发布时间】:2015-01-01 16:45:23
【问题描述】:
我有一个包含 4 个部分的表格视图,每个部分有 1-2 个表格视图单元格。第一个单元格有一个 uiswitch 作为附件视图并控制应用程序的颜色主题,在白天模式和夜间模式之间切换。一旦按下开关,就会调用一个函数,更改导航栏的颜色和背景颜色。在那个函数中,我还写了一行
[self.tableview reloadData];
用新颜色更新表格本身。它工作正常,但没有动画,所以我用这个代替
[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)] withRowAnimation:UITableViewRowAnimationFade];
使用该行时,开关会卡住,应用程序会冻结。它不会崩溃,即没有崩溃日志,它只是冻结并且 uiswitch 停止中间动画。
我注意到我可以重新加载没有包含附件视图的单元格的部分,并且它与淡入淡出动画完美配合。 IE。这行得通
[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;
}
【问题讨论】:
-
请在您的问题中附加崩溃日志。
-
这真的很奇怪,没有崩溃日志,应用程序冻结,它没有崩溃。开关卡在中间,应用程序变得无响应。没有崩溃日志
-
您可以管理表格单元格的对象。并避免在 CellForRawAtIndexPath: 方法中进行大量计算。尽量保持简单。
标签: ios objective-c uitableview reload sections