【发布时间】:2021-03-24 18:38:45
【问题描述】:
我想让tableView 滚动和scrollView 滚动。
我知道有一个解决方案是使用委托,当 tableView 滚动并使 scrollView contentOfSet 发生变化,但这是我不想要的。
UITableView in UIView in UIScrollView. When scrolling tableview don't scroll scrollview too
就像这篇文章,我认为是没有任何设置只初始化视图,任何人都可以告诉我实现这个吗?
这是我初始化视图的方式:
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
}
}
【问题讨论】:
标签: ios swift uitableview uiscrollview