简体   繁体   English

UIScrollView中不可滚动的UITableView

[英]Non scrollable UITableView inside UIScrollView

I have an application in which i want to implement UITableView whose scrollable property set to False. 我有一个应用程序,我想实现其可滚动属性设置为False的UITableView。

I have add that UITableView inside the UIScrollView to achieve this task, Now i want my UIScrollView to scroll it down till the last cell of UITableView. 我在UIScrollView中添加了UITableView来实现这个任务,现在我想让我的UIScrollView向下滚动直到UITableView的最后一个单元格。 So, how can i allocate dynamic height to the UIScrollView so that it will scroll till the last cell of my UITableView. 那么,我如何为UIScrollView分配动态高度,以便它将滚动到我的UITableView的最后一个单元格。

Do you really need scroll view? 你真的需要滚动视图吗? you can simply do this without scroll view 你可以简单地做到这一点,没有滚动视图

tableView.scrollToRow(at: IndexPath(row: lastIndex, section: 0), at: .middle, animated: true)

or 要么

tableView.setContentOffset(CGPoint(x: 0, y: tableView.contentSize.height - tableView.frame.height), animated: true)

Also Apple do not recommend add tableview inside scrollview Apple style guide Apple也不建议在scrollview Apple样式指南中添加tableview

Don't place a scroll view inside of another scroll view. 不要将滚动视图放在另一个滚动视图中。 Doing so creates an unpredictable interface that's difficult to control. 这样做会创建一个难以控制的不可预测的界面。

I have solve this and post my answer here, so that it will help to other developers if they want to achieve this kind of task. 我已经解决了这个问题并在这里发布了我的答案,因此如果他们想要实现这样的任务,它将有助于其他开发人员。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.scrollView.contentSize = CGSize(width: tblHomeTableView.frame.width, height: self.tblHomeTableView.frame.height)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tblHomeTableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
    cell.lblDayCoount.text = "Row: \(indexPath.row + 1)"
    self.tblHomeTableView.frame.size = tblHomeTableView.contentSize
    return cell
}

This will adjust UIScrollView height according to contents in UITableView. 这将根据UITableView中的内容调整UIScrollView高度。

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

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