gpt4 book ai didi

ios - TableView 重新加载部分崩溃

转载 作者:可可西里 更新时间:2023-11-01 03:08:11 24 4
gpt4 key购买 nike

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

[self.tableview reloadData];

用新颜色更新表格本身。它工作正常,但没有动画,所以我改用它

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

当使用该线路时,开关卡住并且应用程序卡住。按说它不会崩溃,即没有崩溃日志,它只是卡住并且 uiswitch 在动画中停止。

我注意到我可以重新加载没有包含附件 View 的单元格的部分,并且它与淡入淡出动画完美配合。 IE。这行得通

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

因为第三部分没有任何带有附属 View 的单元格。但是,如果我尝试重新加载任何具有包含附件 View 的单元格的部分(即第 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;
}

最佳答案

我在包含 UISwitchUITableViewCell 中遇到了同样的问题。我通过在重新加载部分之前调用 [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]

无需先重新加载数据。

关于ios - TableView 重新加载部分崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26758101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com