7

我完全被這個弄糊塗了,無法計算出我錯過了什麼。也許還有很多需要了解核心數據。截取結果控制器中斷搜索顯示

這是我獲取的成果控制器,顯示部分少表,但我的搜索顯示控制器的工作原理:

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                    managedObjectContext:self.buckyballDatabase.managedObjectContext 
                     sectionNameKeyPath:nil 
                       cacheName:nil]; 

鑑於只要我應用sectionNameKeyPath,補充部分。節得到補充,但我的搜索顯示控制器壞了,扔了以下錯誤:

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:1630 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x7481040> 2 indexes [0, 1])' 

這裏是我的表視圖數據源實現filteredList是一個NSArray: -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if (tableView == self.tableView) 
    { 
     return [[self.fetchedResultsController sections] count]; 
    } 
    else 
    { 
     return 1; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.tableView) 
    { 
     return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; 
    } 
    else 
    { 
     return [self.filteredList count]; 
    } 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (tableView == self.tableView) 
    { 
     return [[[self.fetchedResultsController sections] objectAtIndex:section] name]; 
    } 
    else 
    { 
     return nil; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    if (tableView == self.tableView) 
    { 
     if (index > 0) 
     { 
      return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index-1]; 
     } 
     else 
     { 
      self.tableView.contentOffset = CGPointZero; 
      return NSNotFound; 
     } 
    } 
    else 
    { 
     return 0; 
    } 
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    if (tableView == self.tableView) 
    { 
     NSMutableArray *index = [NSMutableArray arrayWithObject:UITableViewIndexSearch]; 
     NSArray *initials = [self.fetchedResultsController sectionIndexTitles]; 
     [index addObjectsFromArray:initials]; 
     return index; 
    } 
    else 
    { 
     return nil; 
    } 
} 

cellForRowAtIndexPath處出現異常:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                  forIndexPath:indexPath]; 

請不要詢問是否需要提供更多的信息.​​.我抓我的頭就這一個了這麼多..

+3

嘗試使用'-dequeueReusableCellWithIdentifier:'和'不-dequeueReusableCellWithIdentifier:forIndexPath:'。 – chris

+0

感謝@chris ...它讓搜索顯示控制器工作。但核心數據中的任何更新(刪除)都會使表中的所有數據都消失,除了索引和段名稱。這個東西在沒有分區和索引的情況下工作:-(。ERROR是:CoreData:錯誤:嚴重的應用程序錯誤。在調用-controllerDidChangeContent時,NSFetchedResultsController的委託捕獲到異常。*** - [__ NSArrayM insertObject:atIndex:] :對象不能與零用戶信息(空) –

+0

試試這個:http://stackoverflow.com/questions/3077332/how-to-refresh-a-uitableviewcontroller-or-nsfetchedresultscontroller – MCKapur

回答

3

嘗試使用-dequeueReusableCellWithIdentifier:而不是-dequeueReusableCellWithIdentifier:forIndexPath:.

+0

爲什麼這個工作?我討厭你,可可; _; – figha

+0

也許這將幫助:http://stackoverflow.com/questions/12714737/where-does-the- indexpath-的-dequeuereusablecellwithidentifierforindexpath,得到 – chris