2011-08-23 60 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