ios - Swift 表格视图返回我的字典的一些单元格
问题描述
大家好,我只想从我的(10k+)个记录字典中显示 100 个单元格。我试图用返回 100 来做到这一点,但它给了我错误 - 索引超出范围,这是正常的行为,但我不知道如何让它像我想要的那样好.. :(
问题是,我只想显示 100 个单元格,但我想搜索每个 dict .. 这是我的代码:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let count = self.items.count
return 100
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item: Price = items[indexPath.row]
tableView.selectRow(at: IndexPath(row: -1, section: 0), animated: false, scrollPosition: UITableViewScrollPosition.none)
showLargePhoto(price: item)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CatalystItem", for: indexPath) as! CatalystTableViewCell
let item: Price = items[indexPath.row]
let appSettings: AppSettings = AppSettings()
cell.lpLabel.text = String(item.no)
cell.catCermaicValueLabel.text = "\(item.ceramWeight) kg"
if appSettings.getHideMetalWeights() == false {
cell.catPtValueLabel.text = item.viewPt ?? ""
cell.catPdValueLabel.text = item.viewPd ?? ""
cell.catRhValueLabel.text = item.viewRh ?? ""
}
else {
cell.catPtValueLabel.text = ""
cell.catPdValueLabel.text = ""
cell.catRhValueLabel.text = ""
}
cell.textCategoryLabel?.text = item.group ?? ""
if FileUtilities.fileExists(nridasnString+".jpg") {
let image: UIImage? = UIImage(named: tmbLocalPath.path)
let scaledImage: UIImage? = image?.scaleToWidth(width: 100)
if cell.imageView != nil {
cell.imageView!.removeFromSuperview()
let iv: UIImageView = UIImageView(frame: CGRect(x: 20, y: 40, width: 100, height: 75))
iv.image = scaledImage
iv.contentMode = .scaleAspectFit
cell.addSubview(iv)
}
}
return cell
}
我不知道怎么做……请杀了我,但我已经完成了……
解决方案
代替
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let count = self.items.count
return 100
}
和
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return min(100,self.items.count)
}
推荐阅读
- python - 如何在 Python 中将列表追加到空列表?
- apache-kafka - vertx kafka客户端的线程模型是什么
- vue.js - 如何在 Centos 7 上安装和运行 vue js 应用程序
- r - “mlm”对象的“stats::confint”置信区间错误?
- java - 如何通过具有特定条件的迭代器从 Set 中删除元素?
- types - 在 Rust 中定义异步函数类型
- visual-studio-code - VS Code 扩展:如何在光标后添加一些东西?
- vue.js - 如何将primevue css文件添加到JHipster Project
- instagram - Instagram 基本显示 API 连接错误
- python - 将 [("['str']", int), ("['str']", int)] 类型的列表转换为 [('str', int), ('str', int)]