![](/img/trans.png)
[英]How can insert cells to top in UITableView without moving to top offset
[英]How to add elements to the top of UITableView without using the insert method?
我有一個類似 Twitter 時間線 controller 的 tableView,所以我收到新消息並且我想在 tableView 上顯示新消息,目前,我將新消息插入到我的數組的第一個索引,然后重新加載 tableView,但這非常昂貴。 我每秒收到大約一百條消息。 如果不將它插入到數組的第一個索引中,我該如何處理呢?
注意:所有消息必須按日期排序。
這是我在評論中建議的簡單版本。 假設一個 UI 包含一個基本表格和一個按鈕。
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet var numberTable: UITableView!
var content = ["1", "2", "3", "4"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return content.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "content_cell", for: indexPath)
let offset = (content.count - 1) - indexPath.row
cell.textLabel?.text = content[offset]
return cell
}
@IBAction func addAnother(_ sender: UIButton) {
content.append("\(content.count + 1)")
numberTable.reloadData()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.