【问题标题】:Swift table view return some cells of my dictionarySwift 表格视图返回我的字典的一些单元格
【发布时间】:2019-02-27 11:21:54
【问题描述】:

大家好,我只想显示我的 (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
    }

我不知道该怎么做……请杀了我,但我已经完成了……

【问题讨论】:

  • 此错误表明您尝试访问的元素超出了数组中可用元素的范围。您确定可以访问 items 数组中的元素吗?
  • 当您收到index out of range 异常时?关于细胞选择或细胞加载?
  • 您确定您的数据有 100 多个值吗?
  • 是的,我确定 :) 我有一个答案。很好,谢谢!
  • 您不应该删除原始问题。其他人可能有同样的问题,并且发现这个问题在未来很有用。

标签: ios swift uitableview nsdictionary


【解决方案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)
}

【讨论】:

  • 谢谢你们,它的工作 :) 我不相信自己 - 那太容易了......:P
【解决方案2】:

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

你有一个美好的一天,欢迎!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2016-04-24
    相关资源
    最近更新 更多