菜鸟教程小白 发表于 2022-12-11 18:41:06

ios - 在表格 View 中向上滚动时 UITableViewCell 为空


                                            <p><ul>
<li><strong>我不滚动 tableView 时的第一张图片</strong></li>
</ul>

<p>我的子项目正在显示</p>

<p> <a href="/image/s0rDr.png" rel="noreferrer noopener nofollow"><img src="/image/s0rDr.png" alt="MY SubItems are Show"/></a> </p>

<ul>
<li><strong>滚动tableView时的第二张图片</strong> </li>
</ul>

<p>我的子项目是空白的</p>

<p> <a href="/image/1GRxc.png" rel="noreferrer noopener nofollow"><img src="/image/1GRxc.png" alt=" MY SubItems are blank "/></a> </p>

<p>我的 CellFor 行代码是 </p>

<pre><code>var aryDictData = []()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

      var aryOptionName = ()
      var strOptionName = String()

      var cell = DeliveryTableViewCell()
      cell = tableView.dequeueReusableCell(withIdentifier: &#34;DeliveryTableViewCell&#34;) as! DeliveryTableViewCell
      cell.selectionStyle = .none

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

         if indexPath.section == 0{
            if indexPath.row &lt; (aryDictData[&#34;storeName&#34;]!).count{

               cell.lblItemName.text = ((aryDictData[&#34;storeName&#34;]! as AnyObject) as! String)

                cell.lblPrice.text = &#34;$\(String(((Double(productPrice)! + grandTotalProductPrice) * Double(numQuntity))))&#34;

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

                let data = aryPreviewOrder as! []
                print(data)

                for i in 0..&lt;((aryDictData[&#34;options&#34;]! as AnyObject)! as AnyObject).count {
                  let optionName = ((((aryDictData[&#34;options&#34;]! as AnyObject)! as AnyObject) as AnyObject)[&#34;option_name&#34;] as! String)
                  let prices = ((((aryDictData[&#34;options&#34;]! as AnyObject)! as AnyObject) as AnyObject)[&#34;price&#34;] as! String)

                  aryOptionName.append(&#34;\(optionName) ($\(prices))&#34;)
                  //                  aryOptionPrices.append(&#34;($\(prices))&#34;)

                  strOptionName = aryOptionName.joined(separator: &#34;\n&#34;)
                  //                  strOptionPrice = aryOptionPrices.joined(separator: &#34;\n&#34;)
                }
                cell.lblOptionNames.text = strOptionName
               }            
         }
   }
    return cell
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尽量避免单元格中的 for 循环,在构建数据源时,您可以创建这些选项名称并将其存储在带有一些键的数组中,然后每次在 cellforrow 中重用该键,而不是再次创建 n再次。</p>

<pre><code>for i in 0..&lt;((aryDictData[&#34;options&#34;]! as AnyObject)! as AnyObject).count {
let optionName = ((((aryDictData[&#34;options&#34;]! as AnyObject)
! as AnyObject) as AnyObject)[&#34;option_name&#34;] as!
String)
let prices = ((((aryDictData[&#34;options&#34;]! as AnyObject)
! as AnyObject) as AnyObject)[&#34;price&#34;] as! String)
aryOptionName.append(&#34;\(optionName) ($\(prices))&#34;)
}
strOptionName = aryOptionName.joined(separator: &#34;\n&#34;)

cell.lblOptionNames.text = strOptionName
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在表格 View 中向上滚动时 UITableViewCell 为空,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46216360/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46216360/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在表格 View 中向上滚动时 UITableViewCell 为空