iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法

1.今天有一个需求,就是在tableview中,可以左滑,出现按钮,然后可以出现一些快捷按钮,比如删除呀,加入收藏夹呀,什么之类的。

2. 以前也做过类似的效果, 不过是接触别人第三方,他们内部是通过UIScrollview来实现的。

3.今天再做的时候,惊喜的发现,原来iOS8.0以后就加入了 UITableviewRowAction这个对象。

4. 现在就是有一个需求,第一行左滑动的时候,不进行相应,只让左滑相应第二行以后的操作。 (类似这种需求,必须要实现-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 这个方法,来自定义可以编辑的indexPath.row)

5.以下是具体代码。

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    YZNewCartSectionModel *model;
    if (indexPath.row > 1) { // 第一行不准左滑, 其余行可以
            return YES;
    }
    return NO;
}

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZNewCartSectionModel *model;
    if (indexPath.section < _dataArray.count) {
        model = _dataArray[indexPath.section];
        if (indexPath.row - 1 < model.sectionProductArray.count ) {
            NSMutableArray *array = [NSMutableArray array];
            UITableViewRowAction *deleAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
               //这个按钮需要处理的代码块
                }
            }];
            
            __weak MBProgressHUD *weakProgress = progress;
            __weak UITableView *weakTable = tableView;
            UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"添加\n喜爱" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
<pre name="code" class="objc"><span style="white-space:pre">		</span>//这个按钮需要处理的代码块
} }]; action.backgroundColor = [UIColor blackColor]; [array addObject:action]; [array addObject:deleAction]; return array; } } return [NSMutableArray new];}

 


大功告成,切记 必须要 8.0以上才能使用这个方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值