tableView - 编辑模式

  • 左划删除按钮
#pragma  mark - UITableViewDelegate
/*
 *只要实现这个代理方法,左划cell删除按钮就有了
 *在编辑模式下:用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时就会调用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(editingStyle == UITableViewCellEditingStyleDelete)
   {
       [self.dealsData removeObjectAtIndex:indexPath.row];
       [self.tableView reloadData];
   }else if (editingStyle == UITableViewCellEditingStyleInsert)
   {
       NSLog(@"点击%zd行的编辑模式下添加按钮",indexPath.row);
   }
}

这里写图片描述

  • 左划自定义按钮
/**
 *  左划cell自定义多个功能选项
 *  8.0后增加的功能
 *  @param tableView 所在的tableView
 *  @param indexPath 左划的行数
 *
 *  @return UITableViewRowAction对象的数组,每一个是对应自定义按钮,比如删除,标记,屏蔽...
 */
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 排列方向从右向左

    UITableViewRowAction *rowaction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"标记" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    }];
    rowaction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *rowaction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    }];
    // normal默认为灰色的
    UITableViewRowAction *rowaction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关心" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

    }];

    return @[rowaction, rowaction1, rowaction2];
}

/**
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

@property (nonatomic, readonly) UITableViewRowActionStyle style;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) UIColor *backgroundColor; // default background color is dependent on style
@property (nonatomic, copy) UIVisualEffect* backgroundEffect;
*/

这里写图片描述

  • 编辑模式
    • 当进入编辑模式tableView每个cell左侧会显示编辑样式:
      删除或者增加
    • 可以通过editingStyleForRowAtIndexPath返回每行的编辑样式,不识闲默认为删除样式
    • 点击删除会先到左划出现的删除按钮
    • 下一步会执行commitEditingStyle方法,对选中行进行操作
// 点击按钮切换编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];

// 返回每行左侧的编辑模式,当不实现的时候默认为删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 偶数行插入模式,奇数行为删除模式
    return indexPath.row % 2 == 0 ?  UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值