美文网首页
iOS开发-两种方式添加cell的删除按钮

iOS开发-两种方式添加cell的删除按钮

作者: DreamerForever | 来源:发表于2017-09-14 15:00 被阅读0次

    第一种方式:

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{        return YES;}- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

    {

    UITableViewRowAction *detele = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

    self.popView = [[ZYFPopview alloc]initInView:[UIApplication sharedApplication].keyWindow tip:@"删除笔记?" images:(NSMutableArray*)@[] rows:(NSMutableArray*)@[@"删除"] doneBlock:^(NSInteger selectIndex) {

    NSFileManager *manager=[NSFileManager defaultManager];

    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"notes.plist"];

    if ([manager removeItemAtPath:filepath error:nil]) {

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [path objectAtIndex:0];

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"notes.plist"];

    [self.notesDatas removeObjectAtIndex:indexPath.row];//bug

    [self.notesDatas writeToFile:plistPath atomically:YES];

    if (self.notesDatas.count==0) {

    [self.tableView removeFromSuperview];//removeFromSuperview将视图从父视图上移开并且销毁,但是如果其他地方对他还有引用,只是移开了视图但是不会销毁

    [self createimgeView];

    }else{

    [self.tableView reloadData];

    }

    }

    } cancleBlock:^{

    }];

    [self.popView showPopView];

    }];

    detele.backgroundColor = [UIColor redColor];

    return @[detele];

    }

    当然这种方式的可以添加多个按钮。

    第二种方式:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;

    }

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    FavoriteModel * model = self.dataSource[indexPath.row];

    [FavoriteModel MR_deleteAllMatchingPredicate:[NSPredicate predicateWithFormat:@"pid=%@",model.pid]];

    [self.dataSource removeObjectAtIndex:indexPath.row];

    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

    }

    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";

    }

    相关文章

      网友评论

          本文标题:iOS开发-两种方式添加cell的删除按钮

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