删除UITableView的最后一部分会导致异常 [英] Deleting the last section of a UITableView causes an exception

查看:94
本文介绍了删除UITableView的最后一部分会导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:"UITableView内部错误:无法生成旧节数为1且新节数为0的新节图"

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal bug: unable to generate a new section map with old section count: 1 and new section count: 0'

下面是代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    @try {
        NSMutableArray *arrData = [dictStartDate* objectForKey:self.navigationItem.title];
        NSLog(@"Title : %@", self.navigationItem.title);
        NSLog(@"Array count : %i", (int)arrData.count);
        return arrData.count;
    }
    @catch (NSException *ex) {
        [[ProjectHandler sharedHandler]LogException:ex.name description:ex.description className:@"TodayTasksViewController" functionName:@"numberOfRowsInSection"];
        return 0;
    }
}

推荐答案

我遇到了这个问题,我测试了它是否删除了该节的最后一行,并解决了这个问题,如果不是删除该行,我删除了该节.我向numberOfSectionsInTableView返回0,没有问题,tableView只是空的.

I had this problem and I solved it testing if I am deleting the last row of a section, and if so instead of deleting the row, I deleted the section. I am returning 0 to the numberOfSectionsInTableView and there is no problem, the tableView is just empty.

贝勒遵循我用来检查它是否是节的最后一行以及关键部分(即删除该节而不是该行)的快速代码.

Bellow follows the swift code I used to check if it is the last row of a section or not and the key part, which is to delete the section and not the row.

假设您在像这样的表格视图中有节的对象:

Suppose you have objects for sections in a table view like this:

var objects: [[Object]]?

然后,如果要使用此对象删除此表的一行,则应该执行以下操作:

Then if you are removing a row of this table with this objects you should do:

objects![indexPath.section].removeAtIndex(indexPath.row)
if (objects![indexPath.section].count == 0) {
    objects!.removeAtIndex(indexPath.section)
    tableView.deleteSections(NSIndexSet.init(index: indexPath.section), withRowAnimation: .Left)
    return //End the func and not execute the next step
}

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left) 

此外,您可以使用类似以下内容的内容来确定它是否是该节中的最后一行:

Also you could use something like this to identify if it's the last row in a section:

tableView.numberOfRowsInSection(indexPath.section)

这篇关于删除UITableView的最后一部分会导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆