tableView强制滚动到底部

在做聊天界面时, 如果发送的内容中有图片加文字,使用

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

这两个方法有可能滚动不到 底部,会有部分没有滚动完

解决方法如下

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

注意的是,这里要判断是否是第一次进入页面, isFirstComing为<2 时 tableView.reloadData 方法执行2次后 停止

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容