Swift table 查看 return 我字典的一些单元格

Swift table view return some cells of my dictionary

大家好,我只想显示我的 (10k+) 条记录字典中的 100 个单元格。我试图用 return 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)
}

当您 return 100 个单元格但没有 100 个元素时,您将获得经典 "array out of items" "out of bounds".

您应该意识到这一点并检查您是否确实有足够的数据来填充这些单元格或减少单元格的数量。

请替换当前委托方法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)
}

祝你今天愉快,欢迎!