tableView強(qiáng)制滾動(dòng)到底部

在做聊天界面時(shí), 如果發(fā)送的內(nèi)容中有圖片加文字,使用

self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
和
self.tableView.setContentOffset(<#T##contentOffset: CGPoint##CGPoint#>, animated: <#T##Bool#>)

這兩個(gè)方法有可能滾動(dòng)不到 底部,會(huì)有部分沒有滾動(dòng)完

解決方法如下

var  isFirstComing  = 0
 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
      var cellHeight: CGFloat = 0.0
      if indexPath.row <= dataArray.count - 1 && isFirstComing < 2 {
       if indexPath.row == dataArray.count - 1 {
          isFirstRefresh += 1
        }
        self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
      
      }
      return cellHeight
    }

并且在viewDidAppear中在刷新一次

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    if self.dataArray.count > 1 {
        self.showEmailTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.dataArray.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: isAnimated)
      }
  }

注意的是,這里要判斷是否是第一次進(jìn)入頁(yè)面, isFirstComing為<2 時(shí) tableView.reloadData 方法執(zhí)行2次后 停止

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容