2017-02-08 68 views
0

我有我的滾動到UITableView底部的方法:添加標題視圖後,滾動的UITableView底部原因NSException

func tableViewScrollToBottom(_ animated: Bool) { 

    let delay = 0.1 * Double(NSEC_PER_SEC) 
    let time = DispatchTime.now() + Double(Int64(delay))/Double(NSEC_PER_SEC) 

    DispatchQueue.main.asyncAfter(deadline: time, execute: { 
     print(self.tview.numberOfRows(inSection: 0)) 
     print(self.tview.numberOfSections) 
     let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0), section: self.tview.numberOfSections) 
     print(indexPath) 
     print("it will crash now") 
     self.tview.scrollToRow(at: indexPath, at: .bottom, animated: animated) 

    }) 
} 

它以前工作,但是當我添加標題視圖 - 它崩潰。

我在控制檯中看到:

8 
1 
[1, 8] 
it will crash now 

,所以我不明白爲什麼這條線:

self.tview.scrollToRow(at: indexPath, at: .bottom, animated: animated) 

導致崩潰。我在這裏錯過了什麼?

+0

如果在你的第8行,那麼最後一行是一行7同樣地,如果有1款則最後一節是第0節。您需要從您嘗試滾動到的索引路徑的行和節中減去1。 – dan

+0

@dan當然你是對的。麻煩添加它作爲答案? – user3766930

回答

1

你有這個

let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0), section: self.tview.numberOfSections) 

問題嘗試做-1

let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0)-1, section: self.tview.numberOfSections-1) 
相關問題