2016-07-27 92 views
1

這裏有一個奇怪的問題。我想我的表視圖滾動到0行的底部第1節中我使用這個代碼:scrollToRowAtIndexPath無法正確滾動到iOS 8的底部

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

它似乎在模擬器完美地工作的IOS版本9.0及以上。但是,在我的設備(iOS 8.1)和8.3的模擬器中,它完全相反。它滾動到單元格的頂部而不是底部!看起來很奇怪。任何想法我可能在這裏做錯了嗎?真的很感激任何指針!謝謝

+1

這似乎是一個錯誤根據[這個答案](http://stackoverflow.com/a/25800080/3769927) – kabiroberai

回答

10

根據this answer對一個類似的問題,它似乎是一個在iOS 8中的錯誤。解決方法是延遲一小部分的滾動(這個代碼也可以在鏈接的答案中找到,但它在Objective-C中)。請注意,我沒有測試此代碼,因爲我沒有安裝iOS 8 Simulator。

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) 
} 

編輯:這是在斯威夫特3答案:

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true) 
} 
+0

你救了我的一天!如果你沒有章節...只是把0(在我的情況) – xhinoda

0

試試這個:

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

我有同樣的問題,當我從UITableViewRowAnimationBottom設置UITableViewScrollPosition選項到UITableViewRowAnimationNone,神奇的發生,對我來說很好,這裏是代碼

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