iOS開發(fā)-兩種方式添加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];

}

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

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容