ios - 重新加载行而不调用 tableview.reload
问题描述
您好,在我的应用程序中,我将数据附加到数组中,然后像这样将行插入到表视图中
self.table.beginUpdates()
self.datasource.append(Data)
self.table.insertRows(at: [IndexPath(row: self.datasource.count-1, section: 0)], with: .automatic)
self.table.endUpdates()
这段代码一切正常,现在当我想刷卡以刷新所有行但我不想使用 table.reloadData 因为它删除所有记录并显示白屏几秒钟,我想重新加载所有没有使用它的行我正在尝试这种方式,但它会使应用程序崩溃
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
if (datasource.count > 0 ){
datasource.removeAll()
page_num = 1
get_past_orders(page: page_num)
self.isLoading = false
refreshControl.endRefreshing()
}
}
应用程序崩溃
self.table.endUpdates()
Thread 1: EXC_BAD_ACCESS (code=1, address=0x20)
解决方案
你的方法是正确的:
您还必须将新项目附加到“项目列表”中
items.append(newItem)
let selectedIndexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [selectedIndexPath], with: .automatic)
tableView.endUpdates()
就这样 :)
编辑:示例
一个非常适合我的小例子:
class ViewController: UIViewController {
struct Item {
var field_a: String
var field_b: Bool
var field_c: Int
}
// MARK: - properties
var items = [Item]()
// MARK: - object-properties
let tableView = UITableView()
let addButton = UIButton()
// MARK: - system-methods
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.addSubview(addButton)
addButton.titleLabel?.text = "ADD"
addButton.titleLabel?.textColor = .white
addButton.backgroundColor = .blue
addButton.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
tableView.dataSource = self
addButton.anchor(top: nil, leading: nil, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: UIEdgeInsets(top: 0, left: 0, bottom: 24, right: 24), size: CGSize(width: 84, height: 48))
tableView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
prepareItems()
}
// MARK: - preparation-methods
func prepareItems() {
items.append(Item(field_a: "cell1", field_b: false, field_c: 0))
items.append(Item(field_a: "cell2", field_b: false, field_c: 0))
items.append(Item(field_a: "cell3", field_b: false, field_c: 0))
}
// MARK: - helper-methods
func appendNewItem(_ item: Item) {
items.append(item)
let selectedIndexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [selectedIndexPath], with: .automatic)
tableView.endUpdates()
}
// MARK: - action-methods
@objc func handleButton() {
appendNewItem(Item(field_a: "new Cell", field_b: true, field_c: 0))
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let currItem = items[indexPath.row]
cell.textLabel?.text = currItem.field_a
return cell
}
}
仅供参考:.anchor() 是我编写的一种方法。- 它设置所有 AutoLayout-Constraints。
推荐阅读
- node.js - 我正在尝试通过 Node.js 在 MySQL 中发布数据,但无法这样做,因为我找不到错误
- firefox - 使用 c# 更改 Firefox 代理设置
- ruby-on-rails - PDF阅读器中的Rails wicked_pdf文件名
- javascript - 如何将 .txt 文档转换为 JavaScript 中的数组?
- azure-devops - Kustomize Azure Devops 文件夹结构
- linux - bash中的for循环无法编辑目录中的所有文件
- r - 当结果页面与搜索页面具有相同的 URL 时,如何对结果页面进行网络抓取?
- javascript - 打字机 JS - .typeString 无法正常工作
- java - 如何从具有通配符返回类型的通用函数将返回值分配给对象?
- http-headers - Angular HttpClient 不发送带有 Content-Type 的请求;应用程序/json 标头