第一種方式:
-(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];
}
當(dāng)然這種方式的可以添加多個按鈕。
第二種方式:
- (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 @"刪除";
}