简体   繁体   English

Swift 表格视图返回我字典的一些单元格

[英]Swift table view return some cells of my dictionary

Hi guys I want to show only 100 cells from my (10k+) records dict.大家好,我只想显示我的(10k+)记录字典中的 100 个单元格。 Im trying to do this with return 100, but its giving me error - index out of range and this is normal behavior for this, but I don't know how to make it good as I want.. :(我试图用 return 100 来做到这一点,但它给了我错误 - 索引超出范围,这是正常的行为,但我不知道如何使它像我想要的那样好..:(

Problem is, that I want to show only 100cells but I want to search for everyone of dict.. here is my code:问题是,我只想显示 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
    }

and I don't know how to make it... kill me please but Im done with it..我不知道怎么做……请杀了我,但我已经完成了……

Replace代替

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let count = self.items.count
    return 100
}

with

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return min(100,self.items.count)
}

When you return 100 cells but you don't have 100 elements you get a classic "array out of items" "out of bounds".当您返回 100 个单元格但没有 100 个元素时,您会得到一个经典的“数组超出项目”“超出范围”。

You should be aware of this and check that you have indeed enough data to fill those cells or either reduce the number of cells.您应该意识到这一点并检查您是否确实有足够的数据来填充这些单元格或减少单元格的数量。

Please replace the current delegate method numberOfRowsInSection :请替换当前的委托方法numberOfRowsInSection

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let count = self.items.count
    return 100
}

by经过

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return min(100,self.items.count)
}

You have a nice day and Welcome!你有一个美好的一天,欢迎!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM