簡體   English   中英

如何在不滾動表的情況下將行插入UITableView的頂部?

[英]How to insert rows to the top of UITableView without scrolling the table?

如何在不滾動表的情況下將行插入UITableView的頂部? 就像當你拖動重新加載推特上的新推文或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()

PS這是我真實項目中的一個示例項目,單元格的大小根據郵件大小而變化。

這是該項目的鏈接: 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圍繞我的第二次更新/結束更新嘗試了它,但仍然顯示頂部是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:使用@beyowulf的答案更新github項目

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並會建議反對它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM