iOS UITableView实现滑动删除、增加、多选
2015-01-19 16:47
701 查看
实现滑动删除,动态增加,以及cell的多选只需要添加cell 的三个代理函数。
1.这个函数是判断每一个cell是否可编辑
-(BOOL)tableView:(UITableView
*)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath
2.这个函数是返回编辑的类型
-(UITableViewCellEditingStyle)tableView:(UITableView
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
3.这个函数是编辑后的操作。但是多选例外。需要借助selected函数
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
废话不多说,直接上代码。。清晰明了
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor
= [UIColor redColor];
self.tableView
= [[UITableView alloc] initWithFrame:CGRectMake(0,
0,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
_tableView.delegate
= self;
_tableView.dataSource
= self;
[self.view
addSubview:_tableView];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑"
style:UIBarButtonItemStyleBordered target:self action:@selector(rightItemEvents:)];
self.navigationItem.rightBarButtonItem
= rightItem;
}
-(void)rightItemEvents:(UIBarButtonItem *)item
{
_tableView.editing
= !_tableView.editing;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中:%d段%d行",indexPath.section,indexPath.row);
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//在这里做你想做的
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"删除");
}else
if (editingStyle == UITableViewCellEditingStyleInsert){
NSLog(@"增加");
}else{
NSLog(@"段:%d",indexPath.section);
}
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//是否允许编辑
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//设置编辑的类型
return UITableViewCellEditingStyleInsert
| UITableViewCellEditingStyleDelete;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
1.这个函数是判断每一个cell是否可编辑
-(BOOL)tableView:(UITableView
*)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath
2.这个函数是返回编辑的类型
-(UITableViewCellEditingStyle)tableView:(UITableView
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
3.这个函数是编辑后的操作。但是多选例外。需要借助selected函数
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
废话不多说,直接上代码。。清晰明了
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor
= [UIColor redColor];
self.tableView
= [[UITableView alloc] initWithFrame:CGRectMake(0,
0,
320, self.view.frame.size.height-64)
style:UITableViewStylePlain];
_tableView.delegate
= self;
_tableView.dataSource
= self;
[self.view
addSubview:_tableView];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑"
style:UIBarButtonItemStyleBordered target:self action:@selector(rightItemEvents:)];
self.navigationItem.rightBarButtonItem
= rightItem;
}
-(void)rightItemEvents:(UIBarButtonItem *)item
{
_tableView.editing
= !_tableView.editing;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中:%d段%d行",indexPath.section,indexPath.row);
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//在这里做你想做的
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"删除");
}else
if (editingStyle == UITableViewCellEditingStyleInsert){
NSLog(@"增加");
}else{
NSLog(@"段:%d",indexPath.section);
}
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//是否允许编辑
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{
//设置编辑的类型
return UITableViewCellEditingStyleInsert
| UITableViewCellEditingStyleDelete;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
相关文章推荐
- iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法
- IOS 开发使用UITableView 实现滑动 删除等多个按钮
- iOS中给UITableView的侧滑删除增加多个按钮的实现方法
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- IOS UITableView 多选,全选删除,拖动,置顶的实现
- ios-day13-01(私人通讯录。控制器之间的数据传递、保存密码和自动登录的实现、普通对象的归档、UITableView的局部刷新和向左滑动删除功能等等)
- ios UITableView实现单击提示,滑动删除,多选行等操作
- [IOS]UITableView实现Cell的滑动删除
- UITableView实现Cell的滑动删除
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
- UITableView实现Cell的滑动删除
- UITableView的多选删除和滑动删除并存
- iOS中使用UITableView的实现向左拉实现删除
- iOS UITableView划动删除的实现
- IOS之UITableView划动删除的实现
- (iOS)UITableView只允许部分cell支持滑动删除
- iOS UItableView+UINavigationController实现单元格的删除
- iOS UItableView+UINavigationController实现单元格的删除
- ios UITableView划动删除的实现
- UITableView的实现过程,滑动,alter弹窗删除对象