美文网首页开发技术文章
IOS实现TableView滑动删除源码

IOS实现TableView滑动删除源码

作者: Johnson_9d92 | 来源:发表于2022-02-07 16:29 被阅读0次

    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;
    }
    

    相关文章

      网友评论

        本文标题:IOS实现TableView滑动删除源码

        本文链接:https://www.haomeiwen.com/subject/fhbmkrtx.html