xcode9.1 ios8.1系统 UITableView的层级结构: Xcode9.1 ios11 系统 UITableView的层级结构:ios
ios8.0之后自定义左划删除功能有个新的方法:(在左边添加两个按钮)xcode
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:nil handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { PLog(@"点击了发标"); CKLWaitChectModel *model = self.allData[indexPath.row]; [self putBorrow:model.ID]; // 收回左滑出现的按钮(退出编辑模式) tableView.editing = NO; }]; UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:nil handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { PLog(@"点击了删除"); CKLWaitChectModel *model = self.allData[indexPath.row]; [self deleteOne:model.ID]; // 收回左滑出现的按钮(退出编辑模式) tableView.editing = NO; }]; return @[action1, action0]; }
实现了上面这个方法之后就不会再执行下面这个方法,由于都是处理左边划出按钮点击事件的spa
(注:ios11 如下的系统,下面这个方法(↓)必须实现,可是里面能够不作任何操做,若是不实现就不会有滑动功能)code
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ PLog(@"这个方法是为了兼容ios11如下的系统,这里面能够不实现任何功能"); }
设置编辑模式:(注:ios11 如下系统左划风格不支持UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete这种格式,若是真想用这种左边选中对勾的风格就得判断下cell是否是左划,而后根据状态给出对应的enum)orm
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; /* UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, 进入编辑状态后左边“减号” ,右边Delete UITableViewCellEditingStyleInsert 进入编辑状态后左边“加号” */ UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete 进入编辑状态后左边就是可选的“对勾”
只要实现上面两个方法后就具有左划功能了,只是右边的按钮效果不是咱们想要的而已,下面咱们就来自定义右边的按钮事件
文章的开头咱们已经了解了tableview的层级结构,ios11之后侧滑视图是直接添加在tableview上的,ios11之前是添加在cell上的,咱们要作出相应的适配(我这里是统一在tableview中捕获修改了,还能够分开来作)ip
我是自定义了tableview,因此写在了layoutsubviews方法里面,没自定义的话就写在viewDidLayoutSubviews里面:rem
- (void)viewDidLayoutSubviews{ [super viewDidLayoutSubviews]; NSLog(@"%s", __func__); // NSLog(@"tableview的子视图:::=++==+= = ==%@", self.subviews); // 获取选项按钮的reference if (@available(iOS 11, *)){ // iOS 11层级 (Xcode 9编译): UITableView -> UISwipeActionPullView for (UIView *subview in self.table.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2) { subview.backgroundColor = [UIColor whiteColor]; CGFloat btnHeight = subview.frame.size.height; CGFloat btnW = 60; UIButton *deleteButton = subview.subviews[1]; UIButton *readButton = subview.subviews[0]; CGRect subFrame = subview.frame; subFrame = CGRectMake(DEVICE_AVALIABLE_WIDTH - 170, subFrame.origin.y, 170, subFrame.size.width); subview.frame = subFrame; deleteButton.frame = CGRectMake(70, (btnHeight - btnW) * 0.5, btnW, btnW); deleteButton.layer.cornerRadius = 3; deleteButton.layer.masksToBounds = YES; [deleteButton.titleLabel removeFromSuperview]; UILabel *deleteLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)]; deleteLB.textAlignment = NSTextAlignmentCenter; deleteLB.text = @"移除"; deleteLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14]; deleteLB.textColor = [UIColor whiteColor]; [deleteButton addSubview:deleteLB]; deleteButton.backgroundColor = [UIColor redColor]; readButton.frame = CGRectMake(0, (btnHeight - btnW) * 0.5, btnW, btnW); readButton.titleLabel.textAlignment = NSTextAlignmentCenter; readButton.layer.cornerRadius = 3; readButton.layer.masksToBounds = YES; [readButton.titleLabel removeFromSuperview]; UILabel *readButtonLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)]; readButtonLB.textAlignment = NSTextAlignmentCenter; readButtonLB.text = @"发标"; readButtonLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14]; readButtonLB.textColor = [UIColor whiteColor]; [readButton addSubview:readButtonLB]; readButton.backgroundColor = [UIColor blueColor]; } } } else{ // iOS 8-10层级: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView UITableViewCell *cell = [self.table cellForRowAtIndexPath:self.editingIndexPath]; for (UIView *subview in cell.subviews){ if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2){ subview.backgroundColor = [UIColor blueColor]; CGFloat btnHeight = subview.frame.size.height; CGFloat btnW = 60; UIButton *deleteButton = subview.subviews[1]; UIButton *readButton = subview.subviews[0]; deleteButton.frame = CGRectMake(70, (btnHeight - btnW) * 0.5, btnW, btnW); deleteButton.layer.cornerRadius = 3; deleteButton.layer.masksToBounds = YES; [deleteButton.titleLabel removeFromSuperview]; UILabel *deleteLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)]; deleteLB.textAlignment = NSTextAlignmentCenter; deleteLB.text = @"移除"; deleteLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14]; deleteLB.textColor = [UIColor whiteColor]; [deleteButton addSubview:deleteLB]; deleteButton.backgroundColor = [UIColor redColor]; readButton.frame = CGRectMake(0, (btnHeight - btnW) * 0.5, btnW, btnW); readButton.titleLabel.textAlignment = NSTextAlignmentCenter; readButton.layer.cornerRadius = 3; readButton.layer.masksToBounds = YES; [deleteButton.titleLabel removeFromSuperview]; UILabel *readButtonLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)]; readButtonLB.textAlignment = NSTextAlignmentCenter; readButtonLB.text = @"发标"; readButtonLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14]; readButtonLB.textColor = [UIColor whiteColor]; [readButton addSubview:readButtonLB]; readButton.backgroundColor = [UIColor yellowColor]; } } } }
注:IOS13cell的层级结构又有变化it
if (@available(iOS 13.0, *)) { if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) { for (UIView *sb in subView.subviews) { if ([sb isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {