0

given that none of the answers here iOS UITableView Scroll to bottom of section work correctly when height of tableview rows varies I've got to ask again: how to I scroll reliably to the bottom of the table? Meaning: the bottom of the table content is lined up with the bottom of the table view

this

extension UITableView
{
func scrollToBottom(animated: Bool = true)
{
    layoutSubviews() // if rowHeight UITableViewAutomaticDimension you have to force layout to get semi-correct content size height
    let csh = contentSize.height
    let fsh = bounds.size.height
    if csh > fsh {
        let offset = csh - fsh
        setContentOffset(CGPointMake(0, offset), animated: animated)
    }
}
}

almost works but still underscrolls by about 150-200 px on both 9.3.1 and 8.4.1

let offset = csh

and it happily overscrolls showing on top the content it underscrolled by in case let offset = csh - fsh followed by the wast whitespace filled with void

given that "bounces vertically" is checked for the tableview in the storyboard it properly bounces in the latter case given a slightest provocation of a user touch ;-)

this is unioslike complex and very much androidlike

this should've been simple, right? right?

Community
  • 1
  • 1
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • What happens if you call scrollToRowAtIndexPath and ask your dataSource for the last index? – Dare Apr 13 '16 at 15:32

1 Answers1

0

scrollToRowAtIndexPath does work with cell of dynamic height. Here's an example:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.lineBreakMode = .ByWordWrapping
        cell.textLabel?.text = [String](count: indexPath.row, repeatedValue: "Hello").joinWithSeparator(" ")
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let lastRow = tableView.numberOfRowsInSection(0)

        tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRow - 1, inSection: 0), atScrollPosition: .Bottom, animated: true)
    }
}
paulvs
  • 11,963
  • 3
  • 41
  • 66