【问题标题】:How to scroll to the bottom cell of tableview in swift 5如何在swift 5中滚动到tableview的底部单元格
【发布时间】:2021-11-17 10:41:59
【问题描述】:

我创建了一个聊天视图,其中我使用了tableview,ChatView的基本功能是显示最新消息(最后一个单元格),但是tableView没有滚动到最后一个索引,它总是卡在中间的某个地方

 func scrollToBottom()  {
    let point = CGPoint(x: 0, y: self.chatTableView.contentSize.height + 200.0)
            self.chatTableView.setContentOffset(point, animated: true)
    }

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    斯威夫特 5

    您好,您只需在加载表格视图后调用这行代码即可。

      self. chatTableView.scrollToRow(at: IndexPath(row: lastRowIndex, section: 0), at: .bottom, animated: true)
    

    【讨论】:

    • 发生此错误(在范围内找不到'lastRowIndex')
    • override func viewDidLoad() { super.viewDidLoad() self.chatTableView.reloadData() self.chatTableView.scrollToRow(at: IndexPath(row: lastRowIndex, section: 0), at: .bottom,动画:真)}
    • 代替“lastRowIndex”,您必须输入tableView最后一行的索引。您可以使用用于填充 tableView 的数组的计数值。例如:(yourArray.count - 1)
    • @s.Aualkh 如果您可以更新您的答案,请将您的最后一条评论移入其中;)谢谢。
    • @s.Aualkh 现在出现此错误 EXC_BAD_ACCESS (code=1, address=0xfffffffffffffffc)
    【解决方案2】:
    override func viewDidLoad() {
        super.viewDidLoad()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
            self.scrollToBottom()
        }
    }
    private func scrollToBottom(){
        let y = self.collectionView.contentSize.height - self.collectionView.btHeight() + self.collectionView.adjustedContentInset.bottom
        self.collectionView.setContentOffset(CGPoint.init(x: 0, y: y), animated: false)
        if self.loadingView.superview != nil {
            self.loadingView.removeFromSuperview()
        }
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多