iOS: 与 insertRowsAtIndexPaths 在 UITableview 中插入多个行
我有 10 行已经在连接中想做什么添加另一个 10 行,我使用的 insertRowsAtIndexPaths,但我要的错误。
以下是我使用的代码
-(void)insertDownloadedActions:(NSMutableArray *)dataToAdd
{
__weak CurrentViewController *weakSelf = self;
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[weakSelf.tableView beginUpdates];
[weakSelf.dataSource addObjects:dataToAdd];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[weakSelf.dataSource count]-dataToAdd.count-1 inSection:0];
[weakSelf.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[weakSelf.tableView endUpdates];
});
}
但我要为那下列错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (20) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
解决方法 1:
代码是密切,但表视图需要确切的对应与什么添加到数据源中要更新的索引路径。
-(void)insertDownloadedActions:(NSMutableArray *)dataToAdd
{
// don't need this
//__weak CurrentViewController *weakSelf = self;
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
// build the index paths for insertion
// since you're adding to the end of datasource, the new rows will start at count
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger currentCount = self.datasource.count;
for (int i = 0; i < dataToAdd.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
}
// do the insertion
[self.dataSource addObjects:dataToAdd];
// tell the table view to update (at all of the inserted index paths)
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
});
}
你想要避免周期哪里块所有者保留块和块 weakSelf (通过使用块所有者"自我") 保留所有者。有的 weakSelf 模式在这里没有必要因为视图控制器不保留在被派遣的块的副本。
iOS: 与 insertRowsAtIndexPaths 在 UITableview 中插入多个行的更多相关文章
- INSERT INTO 语句用于向表格中插入新的行。
语法 INSERT INTO 表名称 VALUES (值1, 值2,....) 我们也可以指定所要插入数据的列: INSERT INTO table_name (列1, 列2,...) VALUES ...
- SQL 在表中插入
SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL INSERT INTO 语句 INSERT INTO 语句用于向表中插入新记录. SQL ...
- SQL语句的使用,SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据
SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法. 但是 SQL 语言也包含用于更新. ...
- iOS学习之UITableView中Cell的操作
接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...
- iOS开发之UITableView中计时器的几种实现方式(NSTimer、DispatchSource、CADisplayLink)
最近工作比较忙,但是还是出来更新博客了.今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结.本篇博客会给出在TableV ...
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
在section=10:row=1:的UITableView中,每一个cell都带有一个按钮,例如如下的图片一样每一个cell中都有一个“进入店铺的按钮”,但是如果我点击相应的cell要进入对应的店铺 ...
- ios UITableView中Cell重用机制导致内容重复解决方法
UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...
- iOS之UITableView中的cell因为重用机制导致新的cell的数据出现重复或者错乱
UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚 ...
- IOS开发UI基础UITableView的属性
UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped <UITableViewDataSour ...
随机推荐
- 你真的懂ThreadPoolExecutor线程池技术吗?看了源码你会有全新的认识
Java是一门多线程的语言,基本上生产环境的Java项目都离不开多线程.而线程则是其中最重要的系统资源之一,如果这个资源利用得不好,很容易导致程序低效率,甚至是出问题. 有以下场景,有个电话拨打系统, ...
- Problem B: 调用函数,求1!+2!+3!+......+10!
#include<stdio.h> double fact(int i); int main() { int i; ; ;i<=;i++) sum=sum+fact(i); prin ...
- FCL研究-目录
准备深入的学习下 FCL,太过于庞大,有些无从下口.用最笨的方法,先从常用的几个命名空间入手. 微软发布了.NET 的源码,学习起来更加方便了. 集合 导航: FCL研究-集合- System.Col ...
- Python+C混编
Python最慢!C最快!Python+C混编?结果可想而知! 樱桃种子 百家号04-1712:11 共享库 使用C语言编译产生共享库,然后python使用ctype库里的cdll来打开共享库. 举例 ...
- Android Design Support Library介绍之:环境搭建
在2015年的GoogleIO大会上.具体的Material Design设计规范出炉了.全新的Android Design Support Library 格.更让人开心的是,这些很酷的风格能够通过 ...
- junit4单元測试总结
junit4单元測试总结 本文开发环境为myeclipse10.7 1. 准备工作 1.1. 选择须要单元測试的文件 创建mavenproject.右击须要单元測试的文件,选择New->oth ...
- perl学习笔记——目录操作
在目录书中移动 chdir 操作副改变当前的工作目录.它和shell中cd命令类似: chdir ‘/etc’ or die “cannot chdir to /etc:$1”; 注意:工作目录不能更 ...
- jsp+servlet实现文件下载
下载 1. 下载就是向客户端响应字节数据! 原来我们响应的都是html的字符数据! 把一个文件变成字节数组,使用response.getOutputStream()来各应给浏览器!!! 2. 下载的要 ...
- Spring整合Activiti工作流
代码地址如下:http://www.demodashi.com/demo/11911.html 一. 前期准备 安装必要的开发环境 eclipse/intellij+maven 3.5.x + tom ...
- java集合类简单思维导图