【问题标题】:Crashed when adding rows in multi section UITableView在多节 UITableView 中添加行时崩溃
【发布时间】:2012-09-24 01:08:37
【问题描述】:

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

我使用的数组是

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”而终止应用程序,原因:“无效更新:第 0 节中的行数无效. 更新后现有节中包含的行数 (6) 必须等于更新前该节中包含的行数 (1),加上或减去从该节中插入或删除的行数 (0已插入,0 已删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。'*

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    在更新表格视图期间,您还必须相应地修改其数据源,据我所知,您的- tableView:numberOfRowsInSection:aryTemp

    因此,在添加过程中,您应该在 [tblView endUpdates]; 行之前进行一些 [aryTemp addObject:...]; 调用,例如:

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

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

    【讨论】:

    • 这个数组有我需要显示的静态值。其他要显示的子项也在其他名为aryTemp1的数组中所以在这种情况下我不会在aryTemp中添加任何东西数组
    • 插入/删除后数据源数组和行数应该仍然匹配。也许您可以只拥有一个单独的数组来保存要在- tableView:numberOfRowsInSection: 中使用的表格视图的内容,并保持当前的aryTemp 不变。
    • 这里我使用了两个数组计数加法。只有当我采用单独的部分而不是在添加/删除行时才会发生这种崩溃
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多