1. 程式人生 > >iOS tableView editCell 刪除Cell 置頂Cell NSIndexPath寫法

iOS tableView editCell 刪除Cell 置頂Cell NSIndexPath寫法

//正常碼tableView 加如下方法:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

//刪除按鈕

UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"刪除"handler:^(UITableViewRowAction * _Nonnull action,

NSIndexPath * _Nonnull indexPath){

        [_dataArrayremoveObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];

    }];

//置頂按鈕

UITableViewRowAction *toTopRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefault

title:@"置頂"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){

NSLog(@"置頂");

        [_dataArrayexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:0];

NSIndexPath *sourceIndexPath = [NSIndexPathindexPathForRow:indexPath.rowinSection:0];

NSIndexPath *destinationIndexPath = [

NSIndexPathindexPathForRow:0inSection:0];

        [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

        tableView.editing = NO;

    }];

    toTopRowAction.backgroundColor = [UIColororangeColor];

//其他按鈕

UITableViewRowAction *otherRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"其他"handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath){

NSLog(@"其他");

        [self.tableViewreloadData];

    }];

    otherRowAction.backgroundColor = [UIColorlightGrayColor];

//返回按鈕陣列

return@[deleteRowAction, toTopRowAction, otherRowAction];

}