2012-10-03 68 views
0

最初我需要在tableview中列出3個項目。
點擊其中的任意一個,它將顯示3個ITems作爲所選主項目的子項目。在多節添加行時崩潰UITableView

陣列我現在用的就是

aryTemp = [[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"YES", @"isExpand", [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil], @"data", nil], nil]; 
aryTemp1 = [[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"NO", @"isExpand", [NSArray arrayWithObjects:@"Doc", @"XML", @"PDF", nil], @"data", nil], nil]; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 3; 
} 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     int count = 0;  
     if(!boolIsExpanded) 
      count = 1; 
     else { 
      count = [[[aryTemp objectAtIndex:0] objectForKey:@"data"] count] + [[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; 
     } 
     return count; 
    } 

    -(void)addOrDeleteRows:(NSIndexPath *)indexPath add:(BOOL)aBool 
    { 
    NSMutableArray *paths = [[NSMutableArray alloc] init]; 

    if(boolIsExpanded) 
    { 
     boolIsExpanded=NO; 
     for (int i=1; i<=[[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) { 
      NSIndexPath *indxPath; 
      indxPath = [NSIndexPath indexPathForRow:(indexPath.row)+i inSection:indexPath.section]; 
      [paths addObject:indxPath]; 
      NSLog(@"paths : %@",paths); 
     } 
     intPrevIndex = indexPath.row; 
     [tblView beginUpdates]; 
     [tblView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft]; 
     [tblView endUpdates]; 
    } 
    else 
    { 
     boolIsExpanded = YES; 
     for (int i = 1; i <= [[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) { 
      NSIndexPath *indxPath = [NSIndexPath indexPathForRow:(indexPath.row)+i inSection:indexPath.section]; 
      [paths addObject:indxPath]; 
     } 
     intPrevIndex = indexPath.row; 
     [tblView beginUpdates]; 
     [tblView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft]; 
     [tblView endUpdates]; 
    }  
    } 

究竟如何,這種計算應該怎麼辦?當使用部分。

插入行後,卡列斯NumberOfRow並得到錯誤 斷言失敗崩潰 - [UITableView的_endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037 2012年10月3日11: 37:43.955 ExpandRowsSample [1657:f803] 由於未捕獲的異常'NSInternalInconsistencyException',原因:'無效的更新:第0節中的行數不正確。更新後現有節中包含的行數(6)必須等於更新前(1)的該部分中包含的行數,加上或減去從該部分插入或刪除的行數(0插入,0刪除),加上或減去移入的行數或者超出該部分(移入0,移出0)。 *

回答

0

在更新表格視圖期間,您還必須相應地修改其數據源,據我所知您的- tableView:numberOfRowsInSection:aryTemp

因此,添加時,你應該有一些[aryTemp addObject:...];呼叫[tblView endUpdates];前行,像:

for (int i=1; i<=[[[aryTemp1 objectAtIndex:0] objectForKey:@"data"] count]; i++) { 
{ 
    // your current code 
    [aryTemp addObject:...]; 
} 

intPrevIndex = indexPath.row; 
[tblView beginUpdates]; 
[tblView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft]; 
[tblView endUpdates]; 

至於刪除行,同樣的概念應適用。

+0

這個數組有我需要顯示的靜態值。其他要顯示的子項也存放在名爲'aryTemp1'的其他數組中。所以在這種情況下,我沒有在'aryTemp'數組中添加任何東西 – Heena

+0

數據源數組和行數在插入/刪除後仍應匹配。也許你可以擁有一個單獨的數組,它保存了table view中用於' - tableView:numberOfRowsInSection:'的內容,並保留當前的'aryTemp'。 – neilvillareal

+0

這裏我使用了兩個數組加法。而且這個崩潰只發生在我獨立的部分,而不是如果我玩添加/刪除行 – Heena