0

我已經基於核心數據書籍示例項目蘋果提供我的應用程序,我已經做的每個小區,顯示某些數據的自定義單元格,作品神奇。核心數據書籍實例添加自定義單元格

不過,現在我想添加第二個自定義單元格。它是一個單獨的單元格,它將始終是表格中的第一個單元格,然後所有從核心數據獲取的結果都會低於該單元格。

我已經試過像這樣:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if (indexPath.row == 0) 
    { 
     static NSString *CellIdentifier = @"statsCell"; 

     GuestStatsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[GuestStatsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     //Configure the cell. 
     [self configureStatsCell:cell]; 

     return cell; 
    } 
    else 
    { 
     static NSString *CellIdentifier = @"guestCell"; 

     customGuestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[customGuestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     // Configure the cell. 
     [self configureGuestCell:cell atIndexPath:indexPath]; 

     return cell; 
    } 
} 

以及一些其他的變化,試圖把事情的工作。幾個問題。

首先,查看加載罰款,我讓我的自定義單元格在索引0,然後我的核心數據單元加載低於。第一個問題,第一個核心數據單元不可見,因爲它隱藏在我的自定義單元后面,但如果我點擊它,我會獲得該核心數據條目的詳細視圖。所以我需要弄清楚如何阻止它進入自定義單元格後面。

我試圖解決這個問題,例如,邏輯上似乎此塊:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

所以,這只是對核心數據記錄設置足夠的細胞,並且不佔額外的定製細胞。所以我儘量簡單,這事:

return [sectionInfo numberOfObjects] + 1; 

那麼這將導致這條線使我得到一個崩潰和索引超出範圍的錯誤:

GuestInfo *guest = [fetchedResultsController objectAtIndexPath:indexPath]; 

這條線是從configureCell方法,設置核心數據記錄單元。所以我非常難以做什麼。

下一個問題是,當我在詳細視圖編輯任何核心數據記錄的屬性,返回的tableview我的自定義單元格不顯示任何變化。它顯示與核心數據信息相關的統計信息。只有其他單元格得到更新。

任何幫助,將不勝感激,如果你需要查看了代碼或需要更多的解釋讓我知道。

編輯:

更多代碼請幫助我。這是NSFetchedController代碼。

- (NSFetchedResultsController *)fetchedResultsController 
{ 

    if (fetchedResultsController != nil) 
    { 
     return fetchedResultsController; 
    } 

    // Create and configure a fetch request with the Book entity. 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuestInfo" inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    // Create the sort descriptors array. 
    NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES]; 
    NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:lastNameDescriptor, firstNameDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    // Create and initialize the fetch results controller. 
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"displayOrder" cacheName:@"Root"]; 

    self.fetchedResultsController = aFetchedResultsController; 
    fetchedResultsController.delegate = self; 

    // Memory management. 
    //No releasing with ARC! 

    return fetchedResultsController; 
}  


/** 
Delegate methods of NSFetchedResultsController to respond to additions, removals and so on. 
*/ 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 

    UITableView *tableView = self.tableView; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureGuestCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      [self.tableView reloadData]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 
} 
+0

你能'ARC'?如果不是,則通過-tableView:cellForRowAtIndexPath:返回的單元格應該是自動釋放的。 – AechoLiu 2012-01-16 03:32:06

+0

您使用過'NSFetchedResultController'。那麼,你能發佈關於配置fetchedResultController的代碼嗎?以及有關[fetchedResultsController performFetch:...]的代碼。順便說一句,在更改託管對象的屬性之後,您是否調用[tableView reloadData]來更新表視圖? – AechoLiu 2012-01-16 03:36:34

+0

是的,我正在使用ARC。調用'[self.tableView reloadData]'有助於解決在單個定製單元上重新加載數據的問題。然而,仍然需要制定出奇怪的單元格重疊,我會張貼您請求的代碼。 – 2012-01-16 11:53:35

回答

0

我正在實現相同的功能,並使用indexPath @ Toro的解決方案工作就好了。不知道有沒有更好的方法,這裏是我對那些感興趣的人的調整。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects] + 1; 
} 

爲我的自定義單元格添加一行,我的提取結果沒有節,所以我這個工作就好了。

- (NSIndexPath *)adjustedIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]; 
    return newPath; 
} 

調整indexpath以從NsFetchedResultController中檢索正確對象的私有方法。

這裏有方法,我申請adjustedIndexPath:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath