最初我需要在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)。 *
這個數組有我需要顯示的靜態值。其他要顯示的子項也存放在名爲'aryTemp1'的其他數組中。所以在這種情況下,我沒有在'aryTemp'數組中添加任何東西 – Heena
數據源數組和行數在插入/刪除後仍應匹配。也許你可以擁有一個單獨的數組,它保存了table view中用於' - tableView:numberOfRowsInSection:'的內容,並保留當前的'aryTemp'。 – neilvillareal
這裏我使用了兩個數組加法。而且這個崩潰只發生在我獨立的部分,而不是如果我玩添加/刪除行 – Heena