Uitableview - Scroll to the Top

UITableView - scroll to the top

UITableView is a subclass of UIScrollView, so you can also use:

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

Or

[mainTableView setContentOffset:CGPointZero animated:YES];

And in Swift:

mainTableView.setContentOffset(CGPointZero, animated:true)

And in Swift 3 & above:

mainTableView.setContentOffset(.zero, animated: true)

iOS TableView reload and scroll top

UItableView method scrollToRow(at:at:animated:) Scrolls through the table view until a row identified by index path is at a particular location on the screen.

Use

tableView.scroll(to: .top, animated: true)

You can use my extension

extension UITableView {

public func reloadData(_ completion: @escaping ()->()) {
UIView.animate(withDuration: 0, animations: {
self.reloadData()
}, completion:{ _ in
completion()
})
}

func scroll(to: scrollsTo, animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
let numberOfSections = self.numberOfSections
let numberOfRows = self.numberOfRows(inSection: numberOfSections-1)
switch to{
case .top:
if numberOfRows > 0 {
let indexPath = IndexPath(row: 0, section: 0)
self.scrollToRow(at: indexPath, at: .top, animated: animated)
}
break
case .bottom:
if numberOfRows > 0 {
let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
self.scrollToRow(at: indexPath, at: .bottom, animated: animated)
}
break
}
}
}

enum scrollsTo {
case top,bottom
}
}

How to scroll to the top of the tableView in swift 4?

Try this!

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

Scroll to top of table view in iOS 11

You can use scrollToRowAtIndexPath method
as -

ObjC

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:true];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:true];
}

SWIFT 4

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: true)
}

You can also set custom row in indexpath by putting various row & section value.

How can I reload UITableView when I scroll to the top with current scroll position? (like chat apps)

Swift 3

This post is a bit old, but here a solution:

1) Detect when you are scrolling near to the top in scrollViewDidScroll,

2) Load the new messages

3) Save the contentSize, reload the tableView

4) Set the contentOffset.y of the tableView to (newContentSizeHeight - oldContentSizeHeight) which is exactly the current point

and here the code:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableView {
if tableView.contentOffset.y < 50 {
loadMessages()
let oldContentSizeHeight = tableView.contentSize.height
tableView.reloadData()
let newContentSizeHeight = tableView.contentSize.height
tableView.contentOffset = CGPoint(x:tableView.contentOffset.x,y:newContentSizeHeight - oldContentSizeHeight)
}
}
}

Scroll to Table View Header top

TableView is a scroll view subclass, so I imagine this should work:

 self.tableView.setContentOffset( CGPoint(x: 0, y: 0) , animated: true)

ViewWillAppear does not get called when app enters foreground. Register you VC for UIApplicationWillEnterForegroundNotification



Related Topics



Leave a reply



Submit