UITableView - scroll to the top

前端 未结 30 3009
南方客
南方客 2020-11-28 17:22

In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section

30条回答
  •  再見小時候
    2020-11-28 17:46

    Since my tableView is full of all kinds of insets, this was the only thing that worked well:

    Swift 3

    if tableView.numberOfSections > 0 && tableView.numberOfRows(inSection: 0) > 0 {
      tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }
    

    Swift 2

    if tableView.numberOfSections > 0 && tableView.numberOfRowsInSection(0) > 0 {
      tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
    }
    

提交回复
热议问题