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];}