iOS开发中iOS8之后UITableViewCell增加的自带

2019-02-02  本文已影响10人  梁森的简书

效果图:

0.左滑.png

iOS8之前UITableViewCell也自带左滑删除功能,但只能显示出一个删除按钮,而iOS增加了新的代理方法以及类让UITableViewCell自带的左滑删除功能能显示多个按钮,就如上图所示有两个按钮:编辑按钮、删除按钮。

iOS8UITableViewDelegate新增的代理方法:

  // This method supersedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

iOS8新增的的类:

  UITableViewRowAction

代理方法的实现:

  // 实现左滑按钮的核心代码
  - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    NSLog(@"点击了删除");
    //        [tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationLeft];
}];
UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
    NSLog(@"点击了编辑");
}];
return @[deleteAction, editAction];
}

要想让系统UITableViewCell能够实现左滑还需实现两个代理方法

  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
editingStyle = UITableViewCellEditingStyleDelete;
}

附上一个小demo:***https://gitee.com/liangsenliangsen/uitableviewcell_left_slide.git

本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊

上一篇下一篇

猜你喜欢

热点阅读