好吧,我一直在这里好几天,似乎无法弄清楚我做错了什么。更新didSelectRowAtIndexPath上的核心数据
我想要的是核心数据更新时,选择一行。我知道了,但是我希望刷新表视图来显示更改。那是我遇到问题的地方。我运行:
[NSFetchedResultsController deleteCacheWithName:@"Root"];
fetchedResultsController = nil;
NSError *error = nil;
if(![[self fetchedResultsController] performFetch:&error]){
}
[[self tabVe] reloadData];
更新数据后,但tableview不更新。我会改变观点并回来,但它被卡住了。我仍然可以滚动并选择更多行,但表格将永远不会刷新。我有一个分段控制器,并且它按照我想要的方式很好地改变了数据,但是我说表格被卡住的原因是因为在选择一行之后,分段控制器不会像它那样更改表格的内容会在选中一行之前。
我不是在编辑模式下这样做,我只是希望它在被选中时更新一个值。这里是我的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tabVe deselectRowAtIndexPath:indexPath animated:YES];
if(cyc == 1){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
}else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Items *anItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
anItem.need = @"0";
NSError *error = nil;
if(![managedObjectContext save:&error]){
}
[NSFetchedResultsController deleteCacheWithName:@"Root"];
fetchedResultsController = nil;
NSError *error = nil;
if(![[self fetchedResultsController] performFetch:&error]){
}
[[self tabVe] reloadData];
}
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[managedObject valueForKey:@"name"] description];
cell.detailTextLabel.text = [[managedObject valueForKey:@"notes"] description];
if([[managedObject valueForKey:@"need"] description] == @"0"){
cell.accessoryType = UITableViewCellAccessoryNone;
}else if([[managedObject valueForKey:@"need"] description] == @"1"){
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
我希望有人能帮我弄明白这一点。 CYC是一个NSInteger表示分段控制器的当前位置,我目前仅更新所述项时CYC等于1
编辑:
一切操作顺利直到
if(![managedObjectContext save:&error]){
}
运行。之后,那就是桌子似乎卡住了。
感谢您的意见。不幸的是,将'fetchedResultsController'设置为'nil'并且没有使用缓存没有使表工作。 此外,我的意思是使用'isEqualToString',但完全忘记了旧的习惯,所以谢谢你提醒我。不知道为什么我使用'description',我只是这么做。 无论如何,感谢您的帮助/建议,但我仍然有相同的问题。 – Shawn
您是否在'fetchedResultsController'的'init'方法中放置了'cacheName:nil'? – Mundi
是的,我把'cacheName:nil'放在我的'fetchedResultsController'的'init'方法中。 – Shawn