您的位置:首页 > 移动开发 > IOS开发

iOS tableView 如何禁止滑动手势删除,只允许在编辑模式下进行删除Cell

2013-11-11 21:12 896 查看
如何禁止滑动手势删除,只允许在编辑模式下进行删除Cell

s设置导航栏上的编辑按钮

- (IBAction)navigationEditButtonClick:(UIButton *)sender {

if (sender.selected == NO) {
sender.selected = YES;
[sender setTitle:@"完成" forState:UIControlStateNormal];
for (int i = 0; i < [_dataArray count];i++) {
UIButton *button = (UIButton *)[self.view viewWithTag:1000 + i];
button.hidden = YES;
UIView *view = (UIView *)[self.view viewWithTag:2000 + i];
view.hidden = YES;
}
_tableView.editing = YES;
}else{
sender.selected = NO;
[sender setTitle:@"编辑" forState:UIControlStateNormal];
for (int i = 0; i < [_dataArray count];i++) {
UIButton *button = (UIButton *)[self.view viewWithTag:1000 + i];
button.hidden = NO;
UIView *view = (UIView *)[self.view viewWithTag:2000 + i];
view.hidden = NO;
}
_tableView.editing = NO;
}
}


实现TableView 的代理方法

#pragma mark - UITableViewDelegate

//指定行是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

//设置tableview是否可编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//这里是关键:这样写才能实现既能禁止滑动删除Cell,又允许在编辑状态下进行删除
if (!tableView.editing)
return UITableViewCellEditingStyleNone;
else {
return UITableViewCellEditingStyleDelete;
}
}

//确定删除某一组的某一行
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:row];
//使用某种动画效果来删除特定的Cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
//删除按钮出现的动画效果
/*
UITableViewRowAnimationAutomatic    //自动匹配
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone

*/
}


这是在今天的项目中遇到的一个细小的问题,但还是费了我不少时间,写下来希望能帮助其他人,同时,使得自己印象更深刻!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: