UITableView - scroll to the top

前端 未结 30 2980
南方客
南方客 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:45

    Adding on to what's already been said, you can create a extension (Swift) or category (Objective C) to make this easier in the future:

    Swift:

    extension UITableView {
        func scrollToTop(animated: Bool) {
            setContentOffset(CGPointZero, animated: animated)
        }
    }
    

    Any time you want to scroll any given tableView to the top you can call the following code:

    tableView.scrollToTop(animated: true)
    

提交回复
热议问题