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

来源:互联网 发布:单片机矩阵按键电路图 编辑:程序博客网 时间:2024/06/13 08:57

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 0
原创粉丝点击