gpt4 book ai didi

ios - 只需在 cellforrow 中进行一次查询即可在 tableview 中快速实现相当大的跳跃

转载 作者:行者123 更新时间:2023-11-29 05:49:27 25 4
gpt4 key购买 nike

我的 willDisplay 中有以下代码:

func tableView(_: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard case let cell as L_LocCell = cell else {
return
}

let subC = subContractors[indexPath.row]
cell.backgroundColor = .clear
cell.locName = subC.companyname
cell.locCode = subC.region

/*if let sType = Helper_Types.getType(subC.type)
{
cell.add = sType.name
}
else {
cell.add = ""
}*/

switch subC.status {
case 3:
cell.catColor = UIColor(rgba: Palette.class_bad)

cell.catName = "Declined"
break
case 2:
cell.catColor = UIColor(rgba: Palette.class_good)
cell.catName = "Approved"
break
case 1:
cell.catColor = UIColor(rgba: Palette.class_warn)
cell.catName = "Pending"
break
case 4:
cell.catColor = UIColor(rgba: Palette.ok)
cell.catName = "Approved with Exception"
break
default:
cell.catColor = companyMed
cell.catName = "Unknown"
break
}

if subConPicDir != nil {
let filename = subConPicDir!.appendingPathComponent(subC.subcontractorId.description+".jpg")
cell.thumbImg.kf.setImage(
with: filename,
placeholder: UIImage(named: "ic_supplier")!,
options: [.transition(.fade(1)), .cacheOriginalImage],
progressBlock: { receivedSize, totalSize in

},
completionHandler: { result in
print(result)

})
}
else{
cell.thumbImg.image = UIImage(named: "ic_supplier")
}

}

当我放回注释掉的部分时,滚动的平滑度有很大的差异。

这只是一个检索一些信息的查询,我没有直接在我的 TableView 的数据源中。我该如何优化它?

public static func getType(_ tid: Int) -> Types?
{
do {
var realm : Realm? = try Realm()
let predicate = NSPredicate(format: "_id = %d", tid);
let types = realm!.objects(STD_type.self).filter(predicate);
realm = nil
if types.count > 0
{
return Types(st : types.first!)
} else {
return nil;
}
} catch let error as NSError {
print("error realm \(error.localizedDescription)")
return nil
}
}

最佳答案

Realm 查找通常非常快,但它仍然是一个异步任务,需要时间,甚至可能在另一个线程上运行。

我建议您不要每次尝试渲染单元格时都执行此操作,而是获取所有类型(可能在 viewDidLoad 中),然后仅在 cellForRow 中进行过滤

然后你可以做类似的事情

cell.add = types.first { $0.id == subC.type } ?? ""

这不是 aysnc,并且速度更快、响应更灵敏

如果由于某种原因你无法获取所有类型,我至少会在你获取结果时缓存它们。

关于ios - 只需在 cellforrow 中进行一次查询即可在 tableview 中快速实现相当大的跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55827561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com