iOS自定义UITableView实现不同系统下的左滑删除功能详解

2020-01-21 07:01:27王冬梅


- (void)layoutSubviews {
 /**自定义设置iOS8-10系统下的左滑删除按钮大小*/
 for (UIView * subView in self.subviews) {
 if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
  subView.backgroundColor = [UIColor clearColor];//去掉默认红色背景
  //设置按钮frame
  CGRect cRect = subView.frame;
  cRect.origin.y = self.contentView.frame.origin.y + 10;
  cRect.size.height = self.contentView.frame.size.height - 20;
  subView.frame = cRect;
  //自定义按钮的文字大小
  if (subView.subviews.count == 1 && self.indexPath.section == 0) {//表示有一个按钮
  UIButton * deleteButton = subView.subviews[0];
  deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
  }
  //自定义按钮的图片
  if (subView.subviews.count == 1 && self.indexPath.section == 1) {//表示有一个按钮
  UIButton * deleteButton = subView.subviews[0];
  [deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];
  [deleteButton setTitle:@"" forState:UIControlStateNormal];
  }
  //自定义按钮的文字图片
  if (subView.subviews.count >= 2 && self.indexPath.section == 0) {//表示有两个按钮
  UIButton * deleteButton = subView.subviews[1];
  UIButton * shareButton = subView.subviews[0];
  [deleteButton setTitle:@"" forState:UIControlStateNormal];
  [shareButton setTitle:@"" forState:UIControlStateNormal];
  [self setUpDeleteButton:deleteButton];
  [self setUpShareButton:shareButton];
  }
 }
 }
}

在iOS11中:

在该系统下由于我们所需自定义的按钮视图UISwipeActionPullView是UITableView的子视图,所以我们可以在控制器中自定义UITableView子类中遍历它的subviews即可(以下方法是写在UITableView的代理方法- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath中的,该代理方法每次会在开始左滑按钮前调用)。代码如下:


/**自定义设置iOS11系统下的左滑删除按钮大小*/
//开始编辑左滑删除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
 NSInteger section = indexPath.section;
 if (@available(iOS 11.0, *)) {
  for (UIView * subView in self.customTableView.subviews) {
   if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
    subView.backgroundColor = [UIColor clearColor];//如果自定义只有一个按钮就要去掉按钮默认红色背景
    //设置按钮frame
    for (UIView * sonView in subView.subviews) {
     if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
      CGRect cRect = sonView.frame;
      cRect.origin.y = sonView.frame.origin.y + 10;
      cRect.size.height = sonView.frame.size.height - 20;
      sonView.frame = cRect;
     }
    }
    //自定义按钮的文字大小
    if (subView.subviews.count == 1 && section == 0) {//表示有一个按钮
     UIButton * deleteButton = subView.subviews[0];
     deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    //自定义按钮的图片
    if (subView.subviews.count == 1 && section == 1) {//表示有一个按钮
     UIButton * deleteButton = subView.subviews[0];
     [deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];;
    }
    //自定义按钮的文字图片
    if (subView.subviews.count >= 2 && section == 0) {//表示有两个按钮
     UIButton * deleteButton = subView.subviews[1];
     UIButton * shareButton = subView.subviews[0];
     [self setUpDeleteButton:deleteButton];
     [self setUpShareButton:shareButton];
    }
   }
  }
 }
}