我有一个带有 TableView 的 UIViewController 用于显示“清单”项目。这个 ViewController 还有一个按钮,可以触发 segue 以模态方式呈现另一个 UIViewController,供用户添加新的清单项。
所有清单项目都存储在 CoreData 中。
新的 CoreData 实体由SecondViewController
. 用户可以checklist
在点击触发解除SecondViewController
. 在这里,由于这SecondViewController
是模态呈现的,我假设它的活动与 根本没有任何关系FirstUIViewController
。
在FirstViewController
包含的 中UITableView
,有一个NSFetchedResultsController
再次在 CoreData 执行获取请求并将所有数据提供给UITableView
. NSFetchedResultsController 的委托也指向 this FirstUIViewController
。
问题是,在解除 后SecondViewController
,UITableView
似乎注意到新清单实体的插入,并UITableCell
在 UITableView 中插入了新实体。然而,奇怪的是,这个新插入的行没有显示正确的cell.textlabel
. UITableViewCell
仅显示标签上没有任何内容的空白。
下面是我如何配置UITableViewCell
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ChecklistCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
ChecklistItem *item = [self.fetch_controller objectAtIndexPath:indexPath];
cell.textLabel.text = item.name;
}
这是我为 NSFetchedResultsControllerDelegate 提供的
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch (type) {
case NSFetchedResultsChangeInsert:
NSLog(@"*** controllerDidChangeObject - NSFetchedResultsChangeInsert");
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
强迫[self.tableView reloadData]
似乎有帮助。我知道这一点是因为我添加了一个按钮来调用此方法,并且单元格的标签相应地被刷新和更新。
因此,我尝试了这个,因为我希望它会在解雇后被调用SecondViewController
。但是,该方法被调用,因为已Reload Data!
记录,但不知何故 tableView 仍然没有正确显示新添加的单元格的标签。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self performFetch];
[self.tableView reloadData];
NSLog(@"Reload Data!");
}
我想知道这里可能有什么问题。不知何故,self.tableView
在解雇SecondViewController
. 有什么建议么?
OK,更奇怪的是我[self.tableView reloadData]
在里面试过- (void)viewDidAppear:(BOOL)animated
。新的 tableViewCell 仍然不能正确显示它的标签;然而,在等待 30 秒后,标签就神奇地弹出了!这是否意味着我的 iPhone/iPhone 模拟器需要时间思考?