在新部分的表视图中插入一行 [英] Inserting a row in a tableview in a new section

查看:65
本文介绍了在新部分的表视图中插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个只有一个部分的表视图开始,第一部分总是有三行.下载了一些内容之后,我想在第二部分中显示一个新的单元格,所以我可以这样做:

I start with a tableview with one section, the first section always has three rows. After some content has downloaded, I want show a new cell, in a second section, so I do :

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

但是我得到一个NSInternalConsistencyException:

But I get an NSInternalConsistencyException:

由于未捕获的异常而终止应用程序"NSInternalInconsistencyException",原因:无效的更新:无效节数.表中包含的节数更新后的视图(2)必须等于部分数更新(1)之前包含在表格视图中的值,加上或减去

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

为什么不自动插入新节,如果我确实插入了节,那么在插入不在新节中的行时会遇到问题.

Why isn't it inserted the new section automatically, If I do insert a section, then I get problems when inserting rows that are not in new sections.

为什么不能仅自动添加新部分?当然,InsertRowAtIndexpath:必要时还包括插入一个节,因为NSIndexPath也包括一个节.

Why can't it just add the new section automatically? surely InsertRowAtIndexpath: also include inserting a section if necessary as an NSIndexPath also includes a section.

推荐答案

[self.tableView insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic];

并且不要忘记相应地设置numberOfSectionsInTavleView和numberOfRowsInSection.

and don't forget to set numberOfSectionsInTavleView and numberOfRowsInSection accordingly.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSInteger numberOfSections;
    if (condition1) {
        numberOfSections = 2;
    } else if (condition2) {
        numberOfSections = 3;
    } else {
        numberOfSections = 4;
    }

    return numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section
    if(section == 1) return 3;
    else return numberOfRows;
}

这篇关于在新部分的表视图中插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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