iOS常用

ios 13 UITableviewCell 左滑删除与UISc

2020-10-27  本文已影响0人  YFBigHeart
问题:横向可滚动的 UIscrollview上面有添加其他VC页面,其中有UITableview
class LCCenterScrollView: UIScrollView,UIGestureRecognizerDelegate {
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer.state.rawValue != 0 {
            return true
        }
        return false
    }
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if indexPath.section == 0 {
            return true
        }
        return false
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        if indexPath.section == 0 , indexPath.row < courses.count {
            let model = courses[indexPath.row]
            let topAction = UITableViewRowAction(style: .default, title: model.isTop ? "取消置顶":"置顶") { [weak self] (rowAction, indexP) in
                guard let `self` = self else {return}
                self.courseTop(model)
            }
            topAction.backgroundColor = MixedColor(styleV: ViewColorSet().V_BBBBBB).unfold()
            let hideAction = UITableViewRowAction(style: .default, title: model.isHided ? "恢复":"隐藏") { [weak self] (rowAction, indexP) in
                guard let `self` = self else {return}
                self.courseHideRequest(model)
            }
            hideAction.backgroundColor = UIColor.zm_color(withHex: 0xff3b3d)
            return [hideAction,topAction]
        }
        return nil
    }
    
  1. 解决左滑 用力过猛 自动调用做右边的Action
 @available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        if indexPath.section == 0 , indexPath.row < courses.count {
            let model = courses[indexPath.row]
            
            let topAction = UIContextualAction(style: .normal, title: model.isTop ? "取消置顶":"置顶") { [weak self] (action, sourceView, complete) in
                guard let `self` = self else {return}
                self.courseTop(model)
                complete(true) // 注意:需要实现这个回调,不然点击后不会自动收缩回去,效果有点僵硬
            }
            
            topAction.backgroundColor = MixedColor(styleV: ViewColorSet().V_BBBBBB).unfold()
            let hideAction = UIContextualAction(style: .normal, title: model.isHided ? "恢复":"隐藏") { [weak self] (action, sourceView, complete) in
                guard let `self` = self else {return}
                self.courseHideRequest(model)
                complete(true)
            }
            hideAction.backgroundColor = UIColor.zm_color(withHex: 0xff3b3d)
            let actions = UISwipeActionsConfiguration(actions: [hideAction,topAction])
            actions.performsFirstActionWithFullSwipe = false
            
            return actions
        }
        return nil
    }

  1. 处理 左滑编辑展开后,向右滑动关闭左滑会触发底部Uiscrollview 页面切换
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        self.superScrollview?.isScrollEnabled = false
}
    
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
       self.superScrollview?.isScrollEnabled = true
}

上一篇下一篇

猜你喜欢

热点阅读