【问题标题】:Reload Table View Except for One Cell重新加载除一个单元格之外的表格视图
【发布时间】:2016-07-06 07:27:57
【问题描述】:

我有一个包含可搜索数据的表格视图和一个包含可编辑文本字段(可使用pickerView 编辑)的单元格。我正在使用 pickerView 来选择要显示的 tableview 部分。但是,当在 pickerView 中选择了新行时,我不希望 pickerView 被关闭,这是我使用时会发生的情况

[self.tableView reloadData];

我尝试了以下方法来解决问题,尝试重新加载表格,但包含 textField 的单元格除外:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSIndexSet *toReload = [[NSIndexSet alloc] initWithIndex:1];
if (row == 0)
{
    self.selectedDepartmentField.text = @"All";
    toReload = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 13)];
}
else
{
    self.selectedDepartmentField.text = [self.departments objectAtIndex:(row - 1)];
}
[self.tableView reloadSectionIndexTitles];
[self.tableView beginUpdates];
[self.tableView reloadSections:toReload withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

这会因 EXC_BAD_ACCESS (code=1,address=0x28) 而崩溃

我应该如何重新加载表中的数据以保持 pickerView 处于活动状态?

【问题讨论】:

  • 你的表中有多少个部分?
  • 当pickerView选择第0行时,会有14个section。当它在任何其他行上时,将有 2 个部分
  • 哪一行触发了崩溃?如有必要,启用 Exceptions 断点以找出答案。
  • [self.tableView endUpdates];

标签: ios objective-c uitableview reloaddata


【解决方案1】:

以下是一种可能的方法

  1. 获取NSIndexPath的数组

    NSArray *visibleIndexPaths = [self.tableView indexPathsForVisibleRows];
    
  2. 使用选择器从visibleIndexPaths 中删除单元格的索引路径。

  3. 使用以下方法重新加载表格视图

    [self.tableView reloadRowsAtIndexPaths:visibleIndexPaths withRowAnimation:UITableViewRowAnimationNone];
    

这应该用选择器重新加载除一个以外的所有可见单元格。

【讨论】:

  • 这对我不起作用,出现错误“尝试将第 0 行插入第 2 部分,但更新后只有 2 个部分”。我认为这可能是因为表格视图更新时行和节较少。
猜你喜欢
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多