将部分插入到 UITableview [英] Insert section to UITableview

查看:23
本文介绍了将部分插入到 UITableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式向 UITableview 添加新部分?

How to add a new section programmatically to UITableview?

我有分页,当我尝试在 performBatchUpdates 中调用 insertSections 时,它第一次工作正常,但是当加载第二页时 UITableview 没有更新(没有错误只是没有更新,也没有调用完成块).

I have pagination and when I try call insertSections in performBatchUpdates it works fine for the first time but when load second page UITableview does't update (no errors just not updated and do not called completion block).

private var periods = [PeriodHistoryModel]()

func fetchHistoryWith(models: [PeriodHistoryModel]) {
    guard models.count > 0 else {
        return
    }

    let newSectionsCount = models.count - periods.count
    let lastPeriod = periods.count - 1 >= 0 ? periods.count - 1 : 0

    var newRows: [IndexPath] = [IndexPath]()
    var newSections: [IndexSet] = [IndexSet]()

    let lastPeriodOldCount = periods.last?.sessions.count ?? 0
    let lastPeriodNewCount = models[lastPeriod].sessions.count
    let newSessionsCount = lastPeriodNewCount - lastPeriodOldCount

    if lastPeriod >= 0 {>
        for index in 0..<newSessionsCount {
            newRows.append(IndexPath(row: lastPeriodOldCount + index, section: lastPeriod))
        }
    }
    for index in 0..<newSectionsCount {
        let newSectionIndex = periods.count + index
        newSections.append(IndexSet(integer: newSectionIndex))
        for rowIndex in 0..<models[newSectionIndex].sessions.count {
            newRows.append(IndexPath(row: rowIndex, section: newSectionIndex))
        }
    }

    periods = models

    tableView.performBatchUpdates({
        for index in 0..<newSections.count {
            tableView.insertSections(newSections[index], with: .none)
        }
        tableView.insertRows(at: newRows, with: .none)
    })
}

reloadData 不适合需要顺利完成

reloadData does not fit need to do it smoothly

推荐答案

重击规则是在执行批量更新之前更新表视图模型".我刚刚创建了一个简单的项目,它运行良好.因此,请确保您的委托方法 numberOfSections 返回更新后的模型计数.

The rule of thump is "update table view models before performing batch updates". I just created a simple project and it works fine. So Make sure your delegate method numberOfSections return the updated model's count.

我的项目如下.

class MyViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var numberOfSections: Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func insertSectionTapped(_ sender: Any) {
        numberOfSections += 1
        let indexSet = IndexSet(integer: numberOfSections - 1)
        tableView.performBatchUpdates({
            tableView.insertSections(indexSet, with: .none)
        }) { (update) in
            print("Update SUccess")
        }
    }

    @IBAction func insertRowTapped(_ sender: Any) {
    }
}

extension MyViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return numberOfSections
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: .default, reuseIdentifier: "DefaultId")
        cell.textLabel?.text = "This is Section \(indexPath.section) - Row \(indexPath.row)"
        return cell
    }
}

这篇关于将部分插入到 UITableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆