如何禁止滑動手勢刪除,只允許在編輯模式下進行刪除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
*/
}
這是在今天的項目中遇到的一個細小的問題,但還是費了我不少時間,寫下來希望能幫助其他人,同時,使得自己印象更深刻!