[英]Swift table view return some cells of my dictionary
大家好,我只想顯示我的(10k+)記錄字典中的 100 個單元格。 我試圖用 return 100 來做到這一點,但它給了我錯誤 - 索引超出范圍,這是正常的行為,但我不知道如何使它像我想要的那樣好..:(
問題是,我只想顯示 100cells 但我想搜索 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)
}
當您返回 100 個單元格但沒有 100 個元素時,您會得到一個經典的“數組超出項目”“超出范圍”。
您應該意識到這一點並檢查您是否確實有足夠的數據來填充這些單元格或減少單元格的數量。
請替換當前的委托方法numberOfRowsInSection :
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)
}
你有一個美好的一天,歡迎!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.