开发技术文章

IOS实现TableView滑动删除源码

2022-02-07  本文已影响0人  Johnson_9d92

IOS实现TableView滑动删除源码

-(void)_deleteSelectIndexPath:(NSIndexPath *)indexPath{
    NSString *filePath =   self.fileArray[indexPath.row];
    NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
     filePath = [docpath stringByAppendingPathComponent:filePath];
    NSLog(@"%@",filePath);
    NSFileManager *fm =  [NSFileManager defaultManager];
    NSError *err;
    if( [fm removeItemAtPath:filePath error:&err] ){
        [self.fileArray removeObjectAtIndex:indexPath.row];
        [self.fileTableView reloadData];
            NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
        //只要实现这个方法,就实现了默认滑动删除!!!!!
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            // 删除数据
            [self _deleteSelectIndexPath:indexPath];
        }
}

//滑动删除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
          // 删除
          return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
      return YES;
}
上一篇 下一篇

猜你喜欢

热点阅读