2013-01-13 25 views
3

我試圖顯示2個部分的表格視圖。第一部分將始終有1行,第二部分將具有與數據點一樣多的行。我使用的核心數據及以下的tableView:numberOfRowsInSection:方法...numberOfRowsInSection:核心數據和多個部分的方法

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

不過,我收到以下錯誤:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

任何幫助將不勝感激。謝謝。

NEW --------------------------------------------- ----------------------------------

這是目前實施的相關方法:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) { 
     cell.textLabel.text = entityOne.name; //entityOne object passed from previous VC 
    } else { 
     entityTwo = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     cell.textLabel.text = entityTwo.name; 
    } 
} 

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex 
    forChangeType:(NSFetchedResultsChangeType)type 
{ 
    NSUInteger frcSectionIndex = 0; 
    frcSectionIndex = sectionIndex + 1; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:frcSectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

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

- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

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

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

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

回答

2

的原因是,所提取的結果控制器(FRC)僅具有一個部分(部#0),要顯示在表視圖的第二部分(部#1)。

這是可能的,但您必須在FRC節號和表格視圖節號之間進行映射,例如,

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

cellForRowAtIndexPath需要相同的映射。

在FRC委託方法didChangeObjectdidChangeSection您對調用表視圖方法(例如insertRowsAtIndexPaths)之前加1到節號。


新增:configureCell應該是這樣的:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) { 
     cell.textLabel.text = entityOne.name; //entityOne object passed from previous VC 
    } else { 
     NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)]; 
     entityTwo = [self.fetchedResultsController objectAtIndexPath:frcIndexPath]; 
     cell.textLabel.text = entityTwo.name; 
    } 
} 

didChangeObject這樣的:

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath 
    forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    UITableView *tableView = self.tableView; 
    NSIndexPath *tvIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section + 1)]; 
    NSIndexPath *tvNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:(newIndexPath.section + 1)]; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:@[tvNewIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:@[tvIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:tvIndexPath] atIndexPath:tvIndexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:@[tvIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:@[tvNewIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

你可能得到的想法:

  • 從FRC索引路徑轉到表視圖索引路徑時,將一個添加到該節。
  • 從表視圖索引路徑到FRC索引路徑時,從該部分中減去1。
+0

謝謝。正如你所解釋的,我已經能夠在numberOfRowsInSection:和didChangeSection:方法中進行映射。但是,我很難在cellForRowAtIndexPath:和didChangeObject:方法中執行映射。你能幫我解決這兩個映射嗎? – AveLeon

+0

@AveLeon:如果您將您當前的這些功能的實現添加到您的問題中,我可以嘗試提供幫助。 –

+0

謝謝。我已經做了。正如您將看到的,我無法通過索引路徑更改方法中的節號。 – AveLeon