从我的 UITableView 中删除部分时出错

Posted

技术标签:

【中文标题】从我的 UITableView 中删除部分时出错【英文标题】:Error while deleting a section from my UITableView 【发布时间】:2012-01-13 15:51:24 【问题描述】:

我需要你的帮助:(

我正在开发一个 ios 应用程序,我必须在其中删除 UItableView 中的一些行和部分。

我在 Xcode 4 上。

我将我的 tableView 与 NSarray 和字典相关联,如下所示:

NSMutableArray *arrTemp1 = [[NSMutableArray alloc]
                         initWithObjects:@"Chris",nil];

    NSMutableArray *arrTemp2 = [[NSMutableArray alloc]
                         initWithObjects:@"Bianca",nil];

    NSMutableArray *arrTemp3 = [[NSMutableArray alloc]
                         initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp4 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp5 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableArray *arrTemp6 = [[NSMutableArray alloc]
                                initWithObjects:@"Candice",@"Clint",@"Chris",nil];

    NSMutableDictionary *temp = [[NSMutableDictionary alloc]
                         initWithObjectsAndKeys:arrTemp1,@"A",arrTemp2,
                                 @"B",arrTemp3,@"C",arrTemp4, @"D", arrTemp5, @"E", arrTemp6, @"J",nil];
    self.tableContents = temp;
    self.sortedKeys =[[self.tableContents allKeys]
                      sortedArrayUsingSelector:@selector(compare:)];

当我需要删除行或部分时,我正在使用此代码 ->

(void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 


    if (editingStyle == UITableViewCellEditingStyleDelete)
       
        NSMutableArray *listData = [self.tableContents objectForKey:
                            [self.sortedKeys objectAtIndex:[indexPath section]]];
        NSUInteger row = [indexPath row];

        [listData removeObjectAtIndex:row];

        [tableView beginUpdates];


        if ([listData count] > 0)
        
            // Section is not yet empty, so delete only the current row.
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
        
        else
        
            // Section is now completely empty, so delete the entire section.
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                     withRowAnimation:UITableViewRowAnimationFade];
        

       [tableView endUpdates];
    

当我删除行时,它运行良好.. 当我删除部分时,我收到以下错误: " * -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit/UIKit-1912.3/UITableView.m:1030 2012-01-13 16:42:45.261 * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后表视图中包含的节数(6)必须等于更新前表视图中包含的节数(6),加上或减去插入或删除的节数(0插入,1已删除)。'"

 (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [tableContents count]; // tableContent is a NSMUtableDictionnary*


- (NSInteger)tableView:(UITableView *)table
 numberOfRowsInSection:(NSInteger)section 

    NSArray *listData =[self.tableContents objectForKey:
                        [self.sortedKeys objectAtIndex:section]];
    return [listData count];

我快疯了,来 *** 寻求帮助...

(对不起我的英语不好:()

感谢大家阅读和回答!!

【问题讨论】:

也许您需要在删除部分之前删除该行?胡思乱想。 我已经尝试过了。而且报错和之前一样,但是这次说的是行数改变了 【参考方案1】:

看起来你实际上并没有删除空数组的字典键,因为它不包含任何对象,因此你调用了

        // Section is now completely empty, so delete the entire section.
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] 
                 withRowAnimation:UITableViewRowAnimationFade];

导致问题,因为您仍然有与以前相同数量的部分(字典键)......

【讨论】:

所以我也要在字典中删除? 是的 removeobjectforkey 应该这样做【参考方案2】:

尝试交换这两行:

[listData removeObjectAtIndex:row];

[tableView beginUpdates];

应该是这样的:

[tableView beginUpdates];

[listData removeObjectAtIndex:row];

【讨论】:

以上是关于从我的 UITableView 中删除部分时出错的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UITableView 中删除一行

UITableView 删除行减少表格行高

删除 UITableView 中的行时出错

如何删除 UITableView 中的空白部分标题

从 Core Data 管理的表视图中删除对象时出错

touchesEnded:withEvent: 没有从我的 UIView 调用,它位于 UITableView 的顶部