1
votes

I have an application in which i want to implement UITableView whose scrollable property set to False.

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. So, how can i allocate dynamic height to the UIScrollView so that it will scroll till the last cell of my UITableView.

2
its a bit complex, you need to have a lot of things in mind, example: you need to calculate the content of your tableView, and adjust the contentSize of your scrollView but is doable - Reinier Melian
yes, I have get the tableview height by using following line self.tableView.frame.size = tableView.contentSize. // this will return width and height of uitableview but then how can i apply it to the scrollview ? - Tejas Patel
set scrollView.contentSize = your Table (width,height) - Reinier Melian
its working perfectly now - Tejas Patel

2 Answers

0
votes

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

Don’t place a scroll view inside of another scroll view. Doing so creates an unpredictable interface that’s difficult to control.

0
votes

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.