通过设置UITableView的属性实现cell的移动,删除,编辑
移动cell
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *str = [self.arr[sourceIndexPath.row]retain];
[self.arr removeObjectAtIndex:sourceIndexPath.row];
[self.arr insertObject:str atIndex:sourceIndexPath.row];
[str release];
}
修改删除按钮的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"";
}
通过左右滑动显示按钮,按钮可以自己编辑
#pragma mark 这个方法是iOS8.0之后出现的方法,可以再编辑状态的时候有多个按钮
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"触发了删除按钮");
}];
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"触发了编辑按钮");
}];
editAction.backgroundColor = [UIColor grayColor];
deleteAction.backgroundColor = [UIColor purpleColor];
return @[deleteAction,editAction];
}
删除数据的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
实现方法
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
#pragma mark 重写系统的编辑按钮点击触发的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:YES];
[self.tableView setEditing:editing animated:YES];
}
#pragma mark 设置哪些行可以进行编辑
if (indexPath.row % 2 == 0) {
return NO;
}
else{
return YES;
}
}