<UIScrollView의 컨텐츠에 UITableView를 사용하여 UITableView의 컨텐츠 사이즈만큼 스크롤링하기>
UIScrollView에 UITableView를 서브뷰로 추가하고, UITableView의 컨텐츠 사이즈만큼 UIScrollView를 스크롤링하는게 목표이다. 이때 UITableView는 스크롤링되지 않는다.
1. 레이아웃 구성


높이는 지정하지 않는다.

테이블뷰의 높이를 추가한후에 Outlet변수를 추가한다.
@IBOutlet var tableViewHeight: NSLayoutConstraint!

UITableView의 스크롤링기능을 모두 끈다.
마지막으로, 테이블뷰의 높이를 테이블뷰의 컨텐츠사이즈로 한다.
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.tableViewHeight.constant = self.tableView.contentSize.height
}
}
----------
전체소스:
class Sub1ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
@IBOutlet var tableViewHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.tableViewHeight.constant = self.tableView.contentSize.height
}
}
}
extension Sub1ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
cell.label.text = "Label \(indexPath.row+1)"
cell.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)
return cell
}
}
댓글 없음:
댓글 쓰기