iOS8之后,UITableViewRowAction实现滑动多个按钮

#pragma mark - View lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier];
}

#pragma mark - UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate Methods

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除-%@", @(indexPath.row));
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    
    UITableViewRowAction *otherAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"置顶-%@", @(indexPath.row));
        [self.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:toIndexPath];
    }];
    
    return @[deleteAction, otherAction];
}


#pragma mark - getter Methods

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        [_dataArray addObjectsFromArray:@[@"Kingdev:row:0",
                                          @"Kingdev:row:1",
                                          @"Kingdev:row:2",
                                          @"Kingdev:row:3",
                                          @"Kingdev:row:4",
                                          @"Kingdev:row:5",
                                          @"Kingdev:row:6",
                                          @"Kingdev:row:7",
                                          @"Kingdev:row:8",
                                          @"Kingdev:row:9",
                                          @"Kingdev:row:10",
                                          @"Kingdev:row:11",
                                          @"Kingdev:row:12"
                                          ]];
    }
    return _dataArray;
}
尊重作者劳动成果,转载请注明: 【kingdev】
原文地址:https://www.cnblogs.com/xiu619544553/p/5606156.html