左滑刪除cell方法:(要使用代理)
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
? ?// 刪除模型
??? [self.wineArray removeObjectAtIndex:indexPath.row];
? ?// 刷新
??? [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
//修改Delete按鈕文字為“刪除”
- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
???return@"刪除";
//??? return [NSString stringWithFormat:@"刪除-%zd", indexPath.row];
}
左滑刪除出現(xiàn)兩個(gè)按鈕的方法:(要使用代理)
按鈕:
- (IBAction)remove {
? ? //刪除模型數(shù)據(jù)
??? [self.wineArray removeObjectAtIndex:0];
??? [self.wineArray removeObjectAtIndex:0];
? ?//刷新
???NSArray*indexPaths = @[? [NSIndexPath indexPathForRow:0inSection:0],? ? [NSIndexPath indexPathForRow:1inSection:0] ];
??? [self.tableView deleteRowsAtIndexPaths:indexPathswithRowAnimation:UITableViewRowAnimationMiddle];
}
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{}
//左滑cell時(shí)出現(xiàn)什么按鈕
- (NSArray*)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath{
???UITableViewRowAction*action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"取消關(guān)注"handler:^(UITableViewRowAction*action,NSIndexPath*indexPath) {
???????NSLog(@"點(diǎn)擊了關(guān)注");
? ? ?// 收回左滑出現(xiàn)的按鈕(退出編輯模式)
??????? tableView.editing=NO;
??? }];
?UITableViewRowAction*action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除"handler:^(UITableViewRowAction*action,NSIndexPath*indexPath) {
//要?jiǎng)h除的哪一行
??????? [self.wineArray removeObjectAtIndex:indexPath.row];
??????? [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
??? }];
編輯模式
#pragma mark -數(shù)據(jù)刷新操作
- (IBAction)remove {
??? [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
#pragma mark - <代理方法>
// 只要實(shí)現(xiàn)了這個(gè)方法娘侍,左滑出現(xiàn)Delete按鈕的功能就有了,點(diǎn)擊了“左滑出現(xiàn)的Delete按鈕”會(huì)調(diào)用這個(gè)方法,點(diǎn)擊了刪除按鈕?
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
???NSLog(@"kkkk");
? ? //刪除模型
??? [self.wineArray removeObjectAtIndex:indexPath.row];
? ? //刷新
??? [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
//修改Delete按鈕文字為“刪除”
- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
???return@"刪除";
}
批量刪除
#pragma mark -數(shù)據(jù)刷新操作
- (IBAction)multipleRemove {//批量刪除按鈕方法
??? [self.tableView setEditing:!self.tableView.isEditing animated:YES];
? ? self.removeButton.hidden= !self.tableView.isEditing;
}
- (IBAction)remove {
????self.tableView.indexPathsForSelectedRows = [0, 1]
? ? //獲得需要?jiǎng)h除的模型數(shù)據(jù)
???NSMutableArray*deletedWineArray = [NSMutableArray array];
???for(NSIndexPath*indexPathin self.tableView.indexPathsForSelectedRows) {
??????? [deletedWineArrayaddObject:self.wineArray[indexPath.row]];
??? }
? ? //刪除模型數(shù)據(jù)
??? [self.wineArray removeObjectsInArray:deletedWineArray];
? ? //刷新
??? [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - <代理方法>
// 只要實(shí)現(xiàn)了這個(gè)方法,左滑出現(xiàn)Delete按鈕的功能就有了,點(diǎn)擊了“左滑出現(xiàn)的Delete按鈕”會(huì)調(diào)用這個(gè)方法
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
???NSLog(@"點(diǎn)擊了代理方法");
? ? //刪除模型
??? [self.wineArray removeObjectAtIndex:indexPath.row];
? ?//刷新
??? [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
//修改Delete按鈕文字為“刪除”
- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{
???return@"刪除";
}