ios - 在不调用 tableview.reload 的情况下重新加载行

标签 ios swift uitableview

你好,在我的应用程序中,我将数据附加到数组中,然后像这样将行插入到 TableView 中

 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 约束。

关于ios - 在不调用 tableview.reload 的情况下重新加载行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52832594/

相关文章:

ios - NSNotificationCenter 与推送通知相关吗?

iOS。如何在使用此 map 执行每个操作后获取有关当前 map 矩形的信息?

ios - 第二个选项卡中的 UICollectionView 导致第一个选项卡中的 UICollectionView 崩溃应用程序

ios - 添加 UISearchController 后 UITableView 可以滚动到顶部太远

ios - 无法插入新的 socket 连接错误

ios - 在 Swift 2、Xcode 7.0.1 中使用 'self'

ios - Cordova 构建使用了错误的资源路径

swift - 如何将数据从 View Controller 传递到弹出 View Controller (swift/ios)

ios - Swift 枚举值不可访问

ios - 将 UITableview 添加为 UIView 的 subview 时出现问题