在UITableView中插入或者删除指定的行(或者节)
2015-09-01 00:00
525 查看
摘要: 在UITableView中插入或者删除指定的行(或者节)
在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对象,通过它我们指定了记录的插入位置。删除 数据也是相同的道理。
在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对象,通过它我们指定了记录的插入位置。删除 数据也是相同的道理。
相关文章推荐
- SSRS 2012 Metro UI 高端报表视频教程【全国首家】
- MySQL树形查询结果封装成Json格式,返回给easyui treegrid
- WebMVC中EasyUI的ComboBox控件的运用
- EasyUI中Messager基本使用
- 设计模式之建造者模式---Builder
- gui学习
- UE4学习之关卡设计入门
- IOS基础UI之(三)手写UI和storyboard方式实现图片移动和缩放
- UITableView
- [leetcode] 115.Distinct Subsequences
- Codeforces GYM 100651 D I Conduit! (水计算几何)
- UIScroll和UIpageControl
- 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数
- 系统引导UEFI 引导,Win下挂载EFI分区教程
- CoreData之FetchRequestController
- UITabBar设置详解
- [转]XML::Parser perl module is required for intltool错误
- *Codeforces Round #316 (Div. 2)- D. Tree Requests (dfs+二分)
- UITextview 垂直居中
- UIday0604:UIControl 的属性和用法