2012-03-31 64 views
1

我有一個應用程序的表格視圖,展開/摺疊部分,遵循Apple的Table View Animations & Gestures sample app中的示例。當一個項目被添加到一個封閉部分時,我遇到了問題:在此之後,部分不再打開,並且當我嘗試打開並關閉它時出現異常。沒有插入UITable的行

我追查這個在打開/關閉方法的一些奇怪的行爲:

-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section { 

if (![[sectionHeaderArray objectAtIndex:section] isOpen]) { 

    [[sectionHeaderArray objectAtIndex:section] setIsOpen:YES]; 

    NSLog(@"self.tableView: %@", self.tableView); 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
    NSInteger countOfRowsToInsert = [sectionInfo numberOfObjects]; 

    NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init]; 
    for (NSInteger i = 0; i < countOfRowsToInsert; i++) { 
     [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]]; 
    } 

    // Apply the updates. 
    [self.tableView beginUpdates]; 
    NSLog(@"Count of rows to insert: %d", [indexPathsToInsert count]); 
    NSLog(@"Rows before insert: %d", [self.tableView numberOfRowsInSection:section]); 
    [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop]; 
    NSLog(@"Rows after insert: %d", [self.tableView numberOfRowsInSection:section]); 
    [self.tableView endUpdates]; 
} 

} 


-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section { 

if ([[sectionHeaderArray objectAtIndex:section] isOpen]) { 

    [[sectionHeaderArray objectAtIndex:section] setIsOpen:NO]; 

    NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:section]; 

    if (countOfRowsToDelete > 0) { 
     NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init]; 
     for (NSInteger i = 0; i < countOfRowsToDelete; i++) { 
      [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]]; 
     } 
     [self.tableView beginUpdates]; 
     NSLog(@"Count of rows to delete: %d", [indexPathsToDelete count]); 
     NSLog(@"Rows before delete: %d", [self.tableView numberOfRowsInSection:section]); 
     [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop]; 
     NSLog(@"Rows after delete: %d", [self.tableView numberOfRowsInSection:section]); 

    } 

    [self.tableView endUpdates]; 
} 
} 

日誌消息表明,開放(插入行),被插入> 0行,但在行數那款保持0:

2012-03-31 13:36:17.454 QuickList7[5523:fb03] Count of rows to insert: 3 
2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows before insert: 0 
2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows after insert: 0 

這將設置不一致的狀態表和數據源之間,然後當我嘗試「崩潰」一節中,我得到以下異常:

2012-03-31 13:48:35.783 QuickList7[5523:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.' 

如何插入3行,仍然以0行結束?

感謝,

薩沙

回答

1

我發現問題了!它實際上在fetchedResultsController的更改處理程序中。它正在響應對封閉部分的更改,導致表格處於不良狀態,並且與數據源不同步。所以我添加了一個檢查每個更新只有插入/刪除/更新行,如果包含部分是打開的。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath 
{ 
UITableView *tv = self.tView; 
switch(type) { 
    case NSFetchedResultsChangeInsert: 
     if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) { 
      [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     break; 

    case NSFetchedResultsChangeDelete: 
     if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) { 
      [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     break; 

    case NSFetchedResultsChangeUpdate: 
     if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) { 
      [self configureCell:[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
     } 
     break; 

    case NSFetchedResultsChangeMove: 
     if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) { 
      [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) { 
      [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     break; 
} 

} 
+0

這是一個棘手的!我遇到了同樣的問題,我花了數小時才發現它與未擴展的部分有關。 – 2014-11-28 20:01:44

0

在我的應用我在一個非常不同的方式,因爲我遇到了這類問題很多實施了類似的行爲。

我有一個表MenuNameCell s,MenuItemCell s和底部的靜態單元格。一次只能擴展一個菜單,點擊MenuNameCell可以展開或摺疊該菜單。由於我將MenuNameCell保留在其自己的部分,MenuItemCell在另一個部分,因此我只需在重新加載表格時插入/刪除整個部分。

這裏是我的表中的數據源:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // number of menus, plus 1 if a menu is open, plus 1 static cell 
    return [self.restaurant.menus count]+(self.menu != nil ? 1 : 0)+1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // if this section is our selected menu, return number of items, otherwise return 1 
    int numberOfRowsInSection = ([self indexPathIsInMenuItemSection:section] ? [[self.menu items] count] : 1); 
    return numberOfRowsInSection; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == [tableView numberOfSections]-1) { 
     // ... set up and return static cell 
    } 
    if ([self indexPathIsInMenuItemSection:indexPath.section]) { 
     // ... set up and return menu item cell 
    } else { 
     // ... set up and return menu name cell 
    } 
} 

和我的表的委託:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    // return if it's a static cell 
    if (indexPath.section==[tableView numberOfSections]-1) 
     return; 
    // if it's a menu name cell, close open menu and maybe expand this menu 
    if (![self indexPathIsInMenuItemSection:indexPath.section]) { 
     BOOL reset = self.menu == m; 
     if (reset) [self reloadTableView:self.tableView withMenu:nil animated:YES autoscroll:NO]; 
     else [self reloadTableView:self.tableView withMenu:m animated:YES autoscroll:YES]; 
    } 
} 

有在那裏提到了幾個幫手:

- (BOOL)indexPathIsInMenuItemSection:(NSInteger)section 
{ 
    // returns YES if section refers to our MenuItemCells 
    int indexOfMenu = [self.restaurant getIndexOfMenu:self.menu]; 
    return indexOfMenu != -1 && section == indexOfMenu+1; 
} 

- (void)reloadTableView:(UITableView *)tableView withMenu:(Menu *)menu animated:(BOOL)animated autoscroll:(BOOL)autoscroll 
{ 
    int oldIndex = [self.restaurant getIndexOfMenu:self.menu]; 
    int newIndex = [self.restaurant getIndexOfMenu:menu]; 

    [tableView beginUpdates]; 

    if (oldIndex != -1) { 
     // index of [section for items] is oldIndex+1 
     [tableView deleteSections:[NSIndexSet indexSetWithIndex:oldIndex+1] withRowAnimation:UITableViewRowAnimationTop]; 
    } 
    if (newIndex != -1) { 
     // index for [section for items] is newIndex+1 
     [tableView insertSections:[NSIndexSet indexSetWithIndex:newIndex+1] withRowAnimation:UITableViewRowAnimationTop]; 
     [self setMenu:menu]; 
    } else { 
     // no new menu 
     [self setMenu:nil]; 
    } 

    [tableView endUpdates]; 
    if (autoscroll) [self autoscroll]; 
} 

- (void)autoscroll 
{ 
    if (self.menu != nil) { 
     int section = [self.restaurant getIndexOfMenu:self.menu]; 
     if (section != -1) { 
      NSUInteger indexes[] = {section,0}; 
      NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:indexes length:2]; 
      [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
     } 
    } 
} 

由於我的數據是在其他地方異步加載,我有這個控制器設置爲接收NSNotification,但它應該工作也可以撥打此號碼viewDidAppear

[self reloadTableView:self.tableView withMenu:self.menu animated:YES autoscroll:YES]; 

我希望這有助於您!讓我知道,如果我能澄清任何它。

+0

謝謝斯賓塞!在查找Apple示例代碼之前,這大致是我期望實現這個目標的過程。雖然沒有想過把標題單元格和內容單元放在不同的部分,但是 - 好主意。我想我會在改變我的方法之前堅持下去,看看是否有人有其他建議(因爲我很懶惰:)。但是,如果我必須重寫,我肯定會使用它作爲模板。謝謝! – Sasha 2012-03-31 19:39:58