在多部分UITableView中添加行时崩溃

时间:2012-10-03 06:18:43

标签: iphone objective-c ios uitableview

最初我需要在tableview中列出3个项目 点击其中的任何一个,它会显示3more ITems作为所选主要项目的子项目。

我正在使用的数组是

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-03 11:37:43.955 ExpandRowsSample [1657:f803] 由于未捕获的异常'NSInternalInconsistencyException'而终止应用,原因:'无效更新:无效的行数在更新(6)之后,现有部分中包含的行数必须等于更新(1)之前该部分中包含的行数,加上或减去从中插入或删除的行数部分(0插入,0删除)和加或减移入或移出该部分的行数(0移入,0移出)。' *

1 个答案:

答案 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];

至于删除行,应该应用相同的概念。