博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在UITableView中动态的插入或删除行(或者节)
阅读量:7075 次
发布时间:2019-06-28

本文共 1305 字,大约阅读时间需要 4 分钟。

在UITableView中插入或者删除指定的行(或者节)使用的是如下几个API:

  • insertRowsAtIndexPath: withRowAnimation: 在指定位置插入行
  • deleteRowsAtIndexPath: withRowAnimation: 删除指定行
  • insertSections: withRowAnimation: 在指定位置插入节
  • deleteSections: withRowAnimation: 删除指定节

调用以上API之前,必须先调用beginUpdates,插入/删除数据完成后再调用endUpdates。

-(IBAction)addRows:(id)sender{

NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

for (int i=0; i<3; i++) {

NSString *s = [[NSString alloc] initWithFormat:@”hello %d”,i];

[datas addObject:s];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

[indexPaths addObject: indexPath];

}

[self.tableView beginUpdates];

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewScrollPositionNone];

[self.tableView endUpdates];

}

-(IBAction)delRows:(id)sender{

NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

[datas removeObjectAtIndex:0];

[indexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:0]];

[self.tableView beginUpdates];

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];

[self.tableView endUpdates];

}

需要注意的是,调用insert函数时,需保证数据源添加的记录数要与你想插入的行的总数一致,如上面的例子中,想要插入的记录有3条,插入位置分 别为1,2,3,则对应的indexpPaths数组的元素总数为3,数组元素为一个NSIndexPath对象,通过它我们指定了记录的插入位置。删除 数据也是相同的道理。

 

转载于:https://www.cnblogs.com/pengyingh/articles/2359419.html

你可能感兴趣的文章
微信天气接口查询
查看>>
spring-前置通知
查看>>
Transient修饰符的使用
查看>>
shell特殊符号,cut、sort、wc、uniq、tee、tr、split命令
查看>>
运维面试题
查看>>
java 消息摘要算法 MAC
查看>>
2011.11.6
查看>>
Linux系统获取命令帮助方法及简单命令介绍
查看>>
PyYAML序列化yaml文件数据
查看>>
Radmin远程连接TMG
查看>>
CCNA 学习笔记(三)--路由选择协议(静态路由协议)
查看>>
python 学习笔记(4)-转载
查看>>
python实例pyspark以及python中文显示
查看>>
一个典型核心网络故障分析
查看>>
获取lamp编译参数
查看>>
Linux系统下启动MySQL的命令及相关知识
查看>>
Shell理论学习(一)
查看>>
phpcms开发之模板语法规则
查看>>
CST UTC
查看>>
因为看见,所以发现:QBotVariant谢绝落幕
查看>>