前言
此次项目中遇到了两类UITableView嵌套使用的场景,里面遇到了一些问题,所以就写此文章记录一下
UITableView 嵌套 UITableView,
首先,设置允许同时识别手势,
class YLGestureTableView: UITableView, UIGestureRecognizerDelegate {
open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
1、UITableView嵌套,从顶部下拉刷新
通过观察视图需要滑动,来控制视图的Contentoffset.y的移动,来达到效果
1.1 设置父视图
此处继承父视图即可,顶部高度可以自己设置高度,通过通知来改变滑动状态,具体滑动哪个
class UpFatherController: UIViewController {
// MARK: - 1.interface
///是否可以滑动
private var canScroll: Bool = true
///顶部高度
public let upTop: CGFloat = 300
// MARK: - 2.lift cycle
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(notiScroll), name: NSNotification.Name("up_father"), object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - 3.private methods
///可以滚动接受
@objc private func notiScroll() {
canScroll = true
}
}
extension UpFatherController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offSetY = scrollView.contentOffset.y
if canScroll == false {
scrollView.contentOffset = CGPoint(x: 0, y: upTop)
}
if offSetY >= upTop {
canScroll = false
NotificationCenter.default.post(name: NSNotification.Name("up_son"), object: nil)
scrollView.contentOffset = CGPoint(x