草庐IT

ios - 在 tableview 中向上滚动时 UITableViewCell 为空

coder 2024-01-29 原文

  • 当我不滚动 tableView 时的第一张图片

我的子项显示

  • 当我滚动 tableView 时的第二张图片

我的子项是空白的

我的 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
}

最佳答案

尽量避免单元格中的 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 - 在 tableview 中向上滚动时 UITableViewCell 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46216360/

有关ios - 在 tableview 中向上滚动时 UITableViewCell 为空的更多相关文章

  1. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  2. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  7. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

  8. ruby - IO::EAGAINWaitReadable:资源暂时不可用 - 读取会阻塞 - 2

    当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab

  9. ruby - 从谷歌开发者网站下载后,client_secret.json 为空 - 2

    我正在尝试从googleAPI下载client_secret.json。我正在执行https://developers.google.com/gmail/api/quickstart/ruby中列出的步骤.使用此向导在GoogleDevelopersConsole中创建或选择项目并自动启用API。在左侧边栏中,选择同意屏幕。选择电子邮件地址并输入产品名称(如果尚未设置),然后单击“保存”按钮。在左侧边栏中,选择凭据并点击创建新客户端ID。选择应用程序类型已安装应用程序,已安装应用程序类型为其他,然后单击“创建客户端ID”按钮。点击新客户端ID下的下载JSON按钮。将此文件移动到您的工作

  10. ruby - 有没有更简洁的方法来检查 PGResult 是否为空? - 2

    我正在使用pggem从Ruby与PostgreSQL对话。有没有检查是否没有结果比使用res.ntuples==0更好的方法?conn=PGconn.connectconfigcmd="select*fromlabelsinnerjoinlabels_mailusing(label_id)"+"wherelabels_mail.mail_id=$1andlabels.name=$2"res=conn.exec(cmd,[mail_id,mailbox])ifres.ntuples==0# 最佳答案 元组,打字错误?使用zero?比=

随机推荐