OStack程序员社区-中国程序员成长平台

标题: ios - 在表格 View 中向上滚动时 UITableViewCell 为空 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:41
标题: ios - 在表格 View 中向上滚动时 UITableViewCell 为空

我的子项目正在显示

MY SubItems are Show

我的子项目是空白的

 MY SubItems are blank

我的 CellFor 行代码是

var aryDictData = [[String:AnyObject]]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var aryOptionName = [String]()
        var strOptionName = String()

        var cell = DeliveryTableViewCell()
        cell = tableView.dequeueReusableCell(withIdentifier: "DeliveryTableViewCell") as! DeliveryTableViewCell
        cell.selectionStyle = .none

        let productPrice = userDefault.object(forKey: ProductDesConstantNames.KAryStorePrices) as! [String]

         if indexPath.section == 0{
            if indexPath.row < (aryDictData[0]["storeName"]!).count{

               cell.lblItemName.text = ((aryDictData[0]["storeName"]! as AnyObject)[indexPath.row] as! String)

                cell.lblPrice.text = "$\(String(((Double(productPrice[indexPath.row])! + grandTotalProductPrice[indexPath.row]) * Double(numQuntity))))"

                cell.lblItemQuantity.text = "\(numQuntity)x"
                cell.lblItemQuantity.textColor = UIColor.init(red: 41/255, green: 144/255, blue: 85/255, alpha: 1.0)

                let data = aryPreviewOrder[indexPath.row] as! [[String:AnyObject]]
                print(data)

                for i in 0..<((aryDictData[0]["options"]! as AnyObject)[indexPath.row]! as AnyObject).count {
                    let optionName = ((((aryDictData[0]["options"]! as AnyObject)[indexPath.row]! as AnyObject)[i] as AnyObject)["option_name"] as! String)
                    let prices = ((((aryDictData[0]["options"]! as AnyObject)[indexPath.row]! as AnyObject)[i] as AnyObject)["price"] as! String)

                    aryOptionName.append("\(optionName) ($\(prices))")
                    //                    aryOptionPrices.append("($\(prices))")

                    strOptionName = aryOptionName.joined(separator: "\n")
                    //                    strOptionPrice = aryOptionPrices.joined(separator: "\n")
                }
                cell.lblOptionNames.text = strOptionName
               }             
           }
     }
    return cell
}



Best Answer-推荐答案


尽量避免单元格中的 for 循环,在构建数据源时,您可以创建这些选项名称并将其存储在带有一些键的数组中,然后每次在 cellforrow 中重用该键,而不是再次创建 n再次。

for i in 0..<((aryDictData[0]["options"]! as AnyObject)[indexPath.row]! as AnyObject).count {
  let optionName = ((((aryDictData[0]["options"]! as AnyObject)
  [indexPath.row]! as AnyObject)[i] as AnyObject)["option_name"] as! 
  String)
  let prices = ((((aryDictData[0]["options"]! as AnyObject)
  [indexPath.row]! as AnyObject)[i] as AnyObject)["price"] as! String) 
  aryOptionName.append("\(optionName) ($\(prices))")
  }
  strOptionName = aryOptionName.joined(separator: "\n")

  cell.lblOptionNames.text = strOptionName

关于ios - 在表格 View 中向上滚动时 UITableViewCell 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216360/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4