如何在不滚动表的情况下将行插入UITableView的顶部?

时间:2016-10-06 17:01:54

标签: ios swift uitableview

如何在不滚动表格的情况下将行插入UITableView的顶部?就像你在Twitter上重新加载新推文或在whatsApp中重新加载消息时一样

我尝试使用begin / endUpdate的另一部分,要求它滚动到第三个索引,但它不起作用。它总是滚动到tableView的顶部。我希望它保持与添加新行之前的位置相同。

    self.tableView.beginUpdates()
    let indexPath = IndexPath(row: 3, section: 0)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
    self.tableView.endUpdates()

P.S。这是我真实项目中的一个示例项目,单元格的大小根据邮件大小而变化。

这是项目的链接: https://github.com/akawther/TableView

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
var loadMore: UIRefreshControl!

var labels = ["A","B","C","D","E","F","G","H","I","J","K"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.loadMore = UIRefreshControl()
    self.loadMore.attributedTitle = NSAttributedString(string: "Pull to reload more")
    self.loadMore.addTarget(self, action: #selector(ViewController.loadMoreChats), for: .valueChanged)
    self.tableView.addSubview(self.loadMore)

    // Do any additional setup after loading the view, typically from a nib.
}

func loadMoreChats() {
    DispatchQueue.main.async(execute: {() -> Void in
        self.tableView.beginUpdates()
        self.labels.insert("3", at: 0)
        self.labels.insert("2", at: 0)
        self.labels.insert("1", at: 0)
        let indexPaths = [
            IndexPath(row: 0, section: 0),
            IndexPath(row: 1, section: 0),
            IndexPath(row: 2, section: 0),
        ]
        self.tableView.insertRows(at: indexPaths, with: .top)
        self.tableView.endUpdates()
        self.loadMore.endRefreshing()

        self.tableView.beginUpdates()
        let indexPath = IndexPath(row: 3, section: 0)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
        self.tableView.endUpdates()
    })
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return labels.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! customCellTableViewCell

    cell.label.text = labels[indexPath.row]
    return cell
}

}

更新#1: 我刚刚尝试了用CATransaction包围我的第二个/ end更新,但仍然显示顶部是1而不是我想要的A:

        CATransaction.begin()
        self.tableView.beginUpdates()
        CATransaction.setCompletionBlock { () -> Void in
            let indexPath = IndexPath(row: 3, section: 0)
            self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
        }
        self.tableView.endUpdates()
        CATransaction.commit()

更新#2: github项目使用@beyowulf

的答案进行更新

1 个答案:

答案 0 :(得分:6)

loadMoreChats是按钮的目标时,此代码对我来说很好:

    func loadMoreChats() {
        self.loadMore.endRefreshing()

        self.labels.insert("3", at: 0)
        self.labels.insert("2", at: 0)
        self.labels.insert("1", at: 0)

        self.tableView.reloadData()
        let indexPath = IndexPath(row: 3, section: 0)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
    }

使用刷新控件的情况,scrollToRow被触发它的平移手势覆盖。

更新

请注意,您引用的应用程序不会以这种方式使用刷新控制器,但如果您坚持使用一个我认为您可以在编程方式滚动表视图时暂时禁用平移手势。类似的东西:

func loadMoreChats() {
    self.loadMore.endRefreshing()

    self.labels.insert("3", at: 0)
    self.labels.insert("2", at: 0)
    self.labels.insert("1", at: 0)

    self.tableView.reloadData()
    let indexPath = IndexPath(row: 3, section: 0)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)

    self.tableView.panGestureRecognizer.isEnabled = false
    let when = DispatchTime.now() + 0.05
    DispatchQueue.main.asyncAfter(deadline: when)
    {
        self.tableView.panGestureRecognizer.isEnabled = true
    }
}

我发现这相当hacky并会建议反对它。