美文网首页
tableView强制滚动到底部

tableView强制滚动到底部

作者: 嗯哼丶傻大个是你 | 来源:发表于2016-12-28 13:07 被阅读0次

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

    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次后 停止

    相关文章

      网友评论

          本文标题:tableView强制滚动到底部

          本文链接:https://www.haomeiwen.com/subject/uhsyvttx.html