【问题标题】:UITableview only scrolling to middle - Swift 4UITableview 只滚动到中间 - Swift 4
【发布时间】:2017-10-15 15:57:40
【问题描述】:

我有一个包含聊天消息的表格视图。我正在尝试滚动到表格视图的底部,但它只滚动了一半。我怀疑这与单元格的高度不同(因为有些消息比其他消息长)有关。

有没有办法完全滚动到底部而不是基于平均单元格高度?

这是我用来滚动到底部的代码:

let lastIndex = IndexPath(row: messages.count - 1, section: 0)
self.tableview.scrollToRow(at: lastIndex, at: .bottom, animated: true)

【问题讨论】:

    标签: ios uitableview scroll swift4


    【解决方案1】:

    使用 TableView 扩展

    import UIKit
    
        extension UITableView {
            func scrollToBottom(animated: Bool) {
                let y = contentSize.height - frame.size.height
                setContentOffset(CGPoint(x: 0, y: (y<0) ? 0 : y), animated: animated)
            }
        }
    

    用法

    tableView.scrollToBottom(animated: true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多