2011-08-23 90 views
1

好吧,我一直在这里好几天,似乎无法弄清楚我做错了什么。更新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]){ 

} 

运行。之后,那就是桌子似乎卡住了。

回答

3

只是猜测,但两件事情浮现在脑海中:

  1. 尝试在我的经验,减少对代码didSelectRowAtIndexPath:这是不够的,只是设置fetchedResultsControllernil,让懒加载照顾的提取。另一个想法是根本不使用任何缓存。这值得一试,如果没有达到性能,这当然是合理的。

  2. 我注意到你在比较两个字符串==。我认为你应该使用isEqualToString:方法进行比较。另外,我不知道为什么除了字符串之外还需要description方法 - NSStringdescription是完全相同的字符串。试着简化这一点。

另外,从您的代码中不清楚变量cyc是什么以及它来自哪里。你可能想要选择更多可理解的变量名称。

+0

感谢您的意见。不幸的是,将'fetchedResultsController'设置为'nil'并且没有使用缓存没有使表工作。 此外,我的意思是使用'isEqualToString',但完全忘记了旧的习惯,所以谢谢你提醒我。不知道为什么我使用'description',我只是这么做。 无论如何,感谢您的帮助/建议,但我仍然有相同的问题。 – Shawn

+0

您是否在'fetchedResultsController'的'init'方法中放置了'cacheName:nil'? – Mundi

+0

是的,我把'cacheName:nil'放在我的'fetchedResultsController'的'init'方法中。 – Shawn