2013-08-02 31 views
0

當點擊節標題時嘗試更新我的tableview時,我得到了這個斷言。在更新uitableview時斷言失敗

*斷言失敗 - [UITableView的_endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-2839.5/UITableView.m:1264

我只是想隱藏和顯示定製單元每當我點擊一個節標題視圖。

如果我用更新數據替換更新代碼,代碼就可以正常工作。但是這並不順暢:(

- (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section 
{ 
    UTNoteItem* noteItem = self.notes[section]; 
    BOOL alreadySelected = noteItem.selected; 
    if (alreadySelected) { 
     self.selectedSection = NSNotFound; 
     [self setSelected:NO forSection:section]; 
    } 
    else { 
     self.selectedSection = section; 
     [self setSelected:YES forSection:section]; 
    } 

    [self updateSections]; 
} 

- (void)setSelected:(BOOL)selected forSection:(NSInteger)section 
{ 
    UTNoteItem* noteItem = self.notes[section]; 
    noteItem.selected = selected; 
    for (UTNoteItem* tmpItem in self.notes) { 
     if (tmpItem != noteItem) { 
      tmpItem.selected = NO; 
     } 
    } 
} 

- (void)updateSections 
{ 
    NSMutableArray* deletePaths = [[NSMutableArray alloc] init]; 
    NSMutableArray* addPaths = [[NSMutableArray alloc] init]; 


    for (UTNoteItem* item in self.notes) { 
     if (item.selected) { 
      [addPaths addObject:[NSIndexPath indexPathForRow:0 inSection:[self.notes indexOfObject:item]]]; 
     } 
     else { 
      [deletePaths addObject:[NSIndexPath indexPathForRow:0 inSection:[self.notes indexOfObject:item]]]; 
     } 
    } 

    [self.tableView beginUpdates]; 
    [self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:YES]; 
    [self.tableView insertRowsAtIndexPaths:addPaths withRowAnimation:YES]; 
    [self.tableView endUpdates]; 

} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.notes.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    UTNoteItem* itemNote = self.notes[section]; 
    if (itemNote.selected) return 1; 
    return 0; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 40; 
} 

編輯:

這是我的新的實現:

-

(void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section 
{ 
    /* Check if a section is opened */ 
    if (self.selectedSection != NSNotFound) { 
     /* A section is open, get the item */ 
     UTNoteItem* theItem = self.notes[self.selectedSection]; 
     /* if the item is the section opened, close it */ 
     if (self.selectedSection == section) { 
      theItem.selected = NO; 
      self.selectedSection = NSNotFound; 

     } 
     /* The item is not the section, so open it, and close the previous item */ 
     else { 
      theItem.selected = YES; 
      UTNoteItem* prevItem = self.notes[self.selectedSection]; 
      prevItem.selected = NO; 
      self.selectedSection = section; 
     } 
    } 
    /* Nothin is open, just open the section */ 
    else { 
     self.selectedSection = section; 
     UTNoteItem* openItem = self.notes[self.selectedSection]; 
     openItem.selected = YES; 
    } 
    /* Reload the selected section.. this will not reload the other sections? */ 
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

回答

1

我也有過類似的問題,但是我執行像重載所以:

- (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)section 
{ 
     //check our action 
     if(<will hide section>) { 
      //hide section 
      <some action> 
     } else { 
      //show section 
      <some action> 
     } 
     [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 

並再次將其重新加載不同用強制更新:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger nRows = 0; //no rows when closed 

    if(<check section is open>) { 
     nRows +=<number of data items shown>; 
    } 

    return nRows; 
} 

凡indexpath.section是我要隱藏或顯示部分。它是平穩和穩定的。 在我看來,刪除和添加行是有點危險的,tableviews非常擅長在各個部分或單元格上進行動畫重新加載。

+0

我其實不會將行用作「標題」。我使用部分作爲我的主標題。所以我的行數是0,直到一個部分被點擊。那麼它應該是1(現在) – chrs

+0

然後請相應地調整它。邏輯是一樣的。你的部分將有0行,如果它是不可見的,並且點擊事件將是 - (void)noteSectionHeader:(UTNoteSectionHeader *)noteSectionHeader sectionTapped:(NSInteger)部分 –

+0

我已經調整了答案以更貼近你的場景,讓我知道這是否有幫助; –