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

问题描述 投票:-2回答:2

嗨,大家好,我想从我的(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
    }

而且我不知道怎么做...请杀了我,但我完成了它...

ios swift uitableview nsdictionary
2个回答
1
投票

更换

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)
}

0
投票

当你返回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)
}

祝你有愉快的一天!

© www.soinside.com 2019 - 2024. All rights reserved.