196,UITableView之cell增删移
来源:互联网 发布:java工程师面试技巧 编辑:程序博客网 时间:2024/06/02 04:32
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *dataList;
@property (nonatomic,strong)UITableView *tableview;
@end
@implementation ViewController
-(NSMutableArray *)dataList{
if (_dataList ==nil) {
_dataList = [NSMutableArrayarrayWithObjects:@"第1行",@"第2行",@"第3行",@"第4行",@"第5行",@"第6行",@"第7行",@"第8行",@"第9行",@"第10行",@"第11行",@"第12行",@"第13行",@"第14行",nil];
}
return_dataList;
}
-(UITableView *)tableview{
if (_tableview ==nil) {
_tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,20, self.view.bounds.size.width,self.view.bounds.size.height - 20) style:UITableViewStylePlain];
_tableview.dataSource =self;
_tableview.delegate =self;
[self.viewaddSubview:_tableview];
}
return_tableview;
}
- (void)viewDidLoad {
[superviewDidLoad];
[selftableview];
// 开始编辑,一旦editing == YES就默认开启删除模式
self.tableview.editing =YES;
}
//设置每一个分组的行总数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataList.count;
}
//设置单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//设置重用ID
static NSString *ID =@"cell";
//从缓存中获取cell
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
//如果缓存中没有cell,那么,就新实例化一个
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];
}
//设置数据
cell.textLabel.text =self.dataList[indexPath.row];
return cell;
}
//只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
//UITableViewCellEditingStyleNone,
//UITableViewCellEditingStyleDelete, 删除
//UITableViewCellEditingStyleInsert 添加
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnullNSIndexPath *)indexPath{
//其实,增加和删除,只要对模型中数据进行添加或删除,再刷新界面,就可以达到效果了
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//1,从数据模型中移除数据
[self.dataListremoveObjectAtIndex:indexPath.row];
//2,刷新界面
//[self.tableview reloadData];这种刷新方法,是界面全部数据刷新,很消耗性能
//下面这种只是局部删掉数据行而已,不会刷新数据
[self.tableviewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
}elseif(editingStyle ==UITableViewCellEditingStyleInsert){
[self.dataListinsertObject:@"第-某-行"atIndex:indexPath.row +1];
//[self.tableview reloadData];
NSIndexPath *path = [NSIndexPathindexPathForRow:indexPath.row +1inSection:indexPath.section];
[self.tableviewinsertRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];
}
}
//返回编辑样式,如果没有实现此方法,默认都是删除。返回UITableViewCellEditingStyleInsert,就是添加了
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row <6){
return UITableViewCellEditingStyleInsert;
}else{
returnUITableViewCellEditingStyleDelete;
}
}
//拖动控件,只需要实现该方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(nonnullNSIndexPath *)sourceIndexPath toIndexPath:(nonnullNSIndexPath *)destinationIndexPath{
//只要改变数据模型中的数据位置就可以了
//1,从源从数组中取出
id source = self.dataList[sourceIndexPath.row];
//2,从源中删除数据
[self.dataListremoveObjectAtIndex:sourceIndexPath.row];
//3,将取出的数据插入数据模型中
[self.dataListinsertObject:source atIndex:destinationIndexPath.row];
}
@end
- 196,UITableView之cell增删移
- UITableView之cell复用
- iOS开发 UITableView之cell
- UITableView Cell
- UITableView Cell
- iOS之UITableView Cell重用机制
- iOS开发:XIB之UITableView自定义cell
- 如何修改一次代码就可以完成多种类型 cell 的 UITableView 增删修改顺序?
- iOS学习之UITableView中Cell的操作
- IOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- IOS开发(25)之删除UITableView的Cell
- IOS开发(28)UITableView之移动Section和Cell
- IOS之UITableView cell自定义视图中插入Tableview
- iOS学习之UITableView中Cell…
- iOS中UITableView学习笔记(二)之cell重用
- IOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- 第一篇blog
- 设计模式-简单工厂
- 【Android】自定义控件实现带百分比显示进度条,可自定义颜色
- POJ 2540 ZOJ 1886 Hotter Colder 半平面交
- 【C#学习】——封装、继承、多态
- 196,UITableView之cell增删移
- [BZOJ3224] Tyvj 1728 普通平衡树
- ubuntu14.04+eclipse(mars)+hadoop-2.7.1开发环境调试程序出现log4j:WARN no appenders could be found for logger。。
- iOS4 - UIPopoverController弹出窗口的位置和坐标(转载)-待整理
- iOS中常用的四种数据持久化方法简介
- 解决spring+Mybatis的整合测试问题
- ubuntu下QT出现cannot find -lGL错误的解决办法
- 为App签名
- 正则表达式的两种创建方式