码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-06-22 10:52:58      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

#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】

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

标签:

原文地址:http://www.cnblogs.com/xiu619544553/p/5606156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!