首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除UITableView中的行要花费太多的时间

删除UITableView中的行要花费太多的时间
EN

Stack Overflow用户
提问于 2017-05-17 14:17:22
回答 1查看 103关注 0票数 0

我正在编写一个包含40k (40000)项数组的程序,并将其显示在UITableView中,搜索表应该过滤并只显示搜索结果。

问题是,一次删除多个行(例如30000+)大约需要10-20秒,并且确保不可能使用。你能建议对这个问题作出什么决定吗?

(tableview.reloadData()不适合)

代码语言:javascript
复制
var allProducts = [Product]()
@IBOutlet weak var searchTableView: UITableView!
@IBOutlet weak var searchTextField: UITextField?

var searchResults = [Product]()

enum Action{
    case Insert
    case Ignore
    case Remove
}

override func viewDidLoad() {
    super.viewDidLoad()
    searchTextField?.addTarget(self, action: #selector(ViewController.textFieldDidChanged(_:)), for: .editingChanged)
    DBBrain().getAllAlcProducts() { [weak self] products in
        self?.allProducts = products
    }
}

func textFieldDidChanged(_ sender: UITextField){
    DispatchQueue.global(qos: .userInitiated).async { [weak self] in
        let text = sender.text!.lowercased()
        let res = self!.allProducts.filter({ $0.name.lowercased().contains(text) })
        if self?.searchTextField?.text != nil && text == self!.searchTextField!.text!{
            if let values = self?.getIndexes(forResults: res){
                self?.searchResults = res
                self?.updateTable(action: values.0, indexes: values.1)
            }
        }
    }
}

private func getIndexes(forResults products: [Product]) -> (Action, [IndexPath]){
    var indexes = [IndexPath]()
    var action = Action.Ignore
    if searchResults.count > products.count{
        var newCounter = 0
        for x in 0..<searchResults.count {
            if products.isEmpty || searchResults[x].id != products[newCounter].id {
                indexes.append(IndexPath(row: x, section: 0))
            }else {
                if newCounter < products.count - 1{
                    newCounter += 1
                }
            }
        }
        action = Action.Remove
    }else if searchResults.count < products.count{
        var oldCounter = 0
        for x in 0..<products.count {
            if searchResults.isEmpty || searchResults[oldCounter].id != products[x].id {
                indexes.append(IndexPath(row: x, section: 0))
            }else {
                if oldCounter < searchResults.count - 1 {
                    oldCounter += 1
                }
            }
        }
        action = Action.Insert
    }
    return (action, indexes)
}

private func updateTable(action: Action, indexes: [IndexPath]) {
    DispatchQueue.main.async { [weak self] in
        if action != .Ignore {
            if action == .Remove {
                self?.searchTableView.beginUpdates()
                self?.searchTableView.deleteRows(at: indexes, with: .fade)
                self?.searchTableView.endUpdates()
            }else if action == .Insert {
                self?.searchTableView.beginUpdates()
                self?.searchTableView.insertRows(at: indexes, with: .fade)
                self?.searchTableView.endUpdates()
            }
        }
    }
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResults.count
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-17 21:08:45

您不应该有一个带有30k+单元格的表。这是一种荒谬的记忆用法。应该对rowsForSection委托方法中的单元格数设置最大限制。

获取您的结果的计数,如果计数大于30左右(即使这是一个很大的数字),将其设置为30。

它可能也有一个超级巨大的阵列。这是一个很大的内存使用,即使你没有为这些做细胞。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44027435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档