3

having an odd issue here. I want to scroll my table view to the bottom of row 0 in section 1. I am using this code:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)

It seems to work perfectly in the simulator for iOS versions 9.0 and above. However in my device (iOS 8.1) and on the simulator for 8.3 it does the complete opposite. It scrolls to the top of the cell and not the bottom! Seems very strange. Any ideas what I might be doing wrong here? Would really appreciate any pointers! thanks

Kex
  • 8,023
  • 9
  • 56
  • 129

3 Answers3

13

According to this answer to a similar question, it seems to be a bug in iOS 8. A workaround would be to delay the scroll by a very minute amount (this code can also be found in the linked answer, but it is in Objective-C). Note that I have not tested this code because I do not have the iOS 8 Simulator installed.

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
    self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)
} 

Edit: Here's the answer in Swift 3:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true)
}
Community
  • 1
  • 1
kabiroberai
  • 2,930
  • 19
  • 34
0

try this one:

if self.tempArray.count == 0 {
 }
else{
   let indexPath = NSIndexPath(forRow: tempArray.count - 1, inSection: 0)
    self.tblChat.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
    }
Chirag Patel
  • 1,453
  • 1
  • 11
  • 21
0

i have the same question,when i set the UITableViewScrollPosition option from UITableViewRowAnimationBottom to UITableViewRowAnimationNone,the magic happend,that works fine for me,here is the code

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.list.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
ChenN
  • 1