首页 > 解决方案 > 重新加载行而不调用 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)

标签: iosswiftuitableview

解决方案


你的方法是正确的:

您还必须将新项目附加到“项目列表”中

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。


推荐阅读