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

第一种方式:

-(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 @"删除";

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容