【问题标题】:How to scroll to the bottom of a UITableView on the iPhone before the view appears如何在视图出现之前滚动到 iPhone 上 UITableView 的底部
【发布时间】:2011-02-15 17:50:14
【问题描述】:

我有一个UITableView,其中填充了可变高度的单元格。当视图被推入视图时,我希望表格滚动到底部。

我目前有以下功能

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[log count]-1 inSection:0];
[self.table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

log 是一个可变数组,包含构成每个单元格内容的对象。

上面的代码在viewDidAppear 中工作正常,但是这有一个不幸的副作用,即当视图首次出现时显示表格的顶部然后跳到底部。如果table view 可以在它出现之前滚动到底部,我会更喜欢它。

我在viewWillAppearviewDidLoad 中尝试了滚动,但在这两种情况下数据都还没有加载到表中并且都抛出异常。

任何指导都将不胜感激,即使只是告诉我我所拥有的一切都是可能的。

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    我相信旧的解决方案不适用于 swift3。

    如果您知道表格中的行数,您可以使用:

    tableView.scrollToRow(
        at: IndexPath(item: listCountInSection-1, section: sectionCount - 1 ), 
        at: .top, 
        animated: true)
    

    【讨论】:

      【解决方案2】:

      我又找到了一个好办法,如下:

      func yourFunc() {
          //tableView.reloadData()
          self.perform(#selector(scrollToBottom), with: nil, afterDelay: 0.1)
      }
      
      @objc func scrollToBottom() {
          self.tableView.scrollToRow(at: IndexPath(row: 0, section: self.mesArr.count - 1), at: .bottom, animated: false)
      }
      

      【讨论】:

        【解决方案3】:

        在 tableView 底部滚动最安全的方法是使用“tableView.scrollRectToVisible”。调用 tableView.reloadData() 后使用。 在 Swift 5 中

        private func scrollAtTheBottom() {
            let lastIndexPath = IndexPath(row: state.myItemsArray.count - 1, section: 0)
            let lastCellPosition = tableView.rectForRow(at: lastIndexPath)
            tableView.scrollRectToVisible(lastCellPosition, animated: true)
        }
        

        【讨论】:

          【解决方案4】:

          斯威夫特 5 这是解决这个问题的简单方法

          步骤:-> 1- 当 ViewDidLoad() 时,它会滚动 2- customTableView 是 IBoutlet 的 tableView var data: [String] = ["Hello", "This","is","Your","World"]

             override func viewDidLoad() {
             super.viewDidLoad()
            
          3- In viewDidLoad we need to tell about indexPath
             /// data.count-1 will give last cell 
             let indexPath = IndexPath(row: data.count - 1, section:0)
          
             /// 1st Parameter: (at) will use for indexPath
             /// 2nd Parameter: (at) will use for position's scroll view
             /// 3rd Parameter: (animated) will show animation when screen will appear
          
             customtableView.scrollToRow(at:indexPath, at:.top, animated:true)
              /// Reload CustomTableView
             customTableView.reloadData()
          

          【讨论】:

            【解决方案5】:

            在 Swift 中,你只需要

            self.tableView.scrollToNearestSelectedRowAtScrollPosition(UITableViewScrollPosition.Bottom, animated: true)
            

            让它自动滚动到按钮

            【讨论】:

              猜你喜欢
              • 2019-10-12
              • 2018-10-25
              • 2011-07-05
              • 1970-01-01
              • 2016-07-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多