繁体   English   中英

UITableView 在 UIView 在 UIScrollView。 滚动表格视图和滚动滚动视图时

[英]UITableView in UIView in UIScrollView. When scrolling tableview and scroll scrollview too

我想在 tableView 滚动和 scrollView 滚动时进行。

我知道有一个解决方案是使用委托,当 tableView 滚动并使 scrollView contentOfSet 更改但这是我不想要的。

UITableView 在 UIView 在 UIScrollView。 滚动表格视图时也不要滚动滚动视图

就像这篇文章一样,我认为是没有任何设置只初始化视图,任何人都可以告诉我实现这个吗?

这是我初始化视图的方式:

import UIKit

class ViewController: UIViewController {
    
    let backView = UIScrollView()
    let tableView = UITableView()
    let testView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(backView)
        backView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        backView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 1000)
        
        backView.addSubview(testView)
        testView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500)
        
        testView.addSubview(tableView)
        tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500)
        
        tableView.dataSource = self        
        
    }

}

extension ViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    
}

如果您想要层次结构中的多个滚动视图(tableview/scrollview),那么您应该使内部滚动视图不可滚动。

在您的情况下,tableview 的高度应等于其内容的高度,以便它不再可滚动。 但是父滚动视图将有助于滚动。 这将防止滚动冲突。

使用 ContentSizesTableView 代替 TableView。

class ContentSizedTableView: UITableView {
    override var contentSize: CGSize {
        didSet{
            self.invalidateIntrinsicContentSize()
        }
    }

    override var intrinsicContentSize: CGSize {
        self.layoutIfNeeded()
        return contentSize
    }

    override func reloadData() {
        super.reloadData()
        invalidateIntrinsicContentSize()
        layoutIfNeeded()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM