ios - 从我的 UITableView 中删除一个部分时出错

标签 ios objective-c uitableview rows

我需要你的帮助:(

我正在开发一个 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”而终止应用程序,原因:“无效更新:无效的部分数。更新后 TableView 中包含的节数(6)必须等于更新前 TableView 中包含的节数(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];
}

我快疯了,来到 stackOverflow 寻求帮助...

(抱歉我的英语不好 :( )

感谢大家阅读和回答!!

最佳答案

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

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

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

关于ios - 从我的 UITableView 中删除一个部分时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8853271/

相关文章:

ios - 如何固定tableFooterView的位置

iphone - 子类化 UIViewControllers View

iphone - 访问 iPhone 用户歌曲和视频?

iOS 十六进制加法

ios - UIDocumentInteractionController 注解属性使用

ios - 自定义 UITableViewCell 不工作(drawRect :) on device [Swift]

iphone - 用于 iOS 项目的 XMLRPC-iOS

ios - 以 NSDictionary 作为对象的嵌套数组上的 NSPredicate

objective-c - 弱属性在 dealloc 中设置为 nil 但属性的 ivar 不是 nil

objective-c - 如何在 UITableViewCell 的 UITextView 中一致地绘制 NSAttributedString