如何将数组导入函数 tableViewControler

内达·德拉赫谢什

我正在一个 swift 项目中的 tableViewControler 中实现一个表视图。我正在从 viewDidLoad 中的 json 调用创建两个数组,viewDidLoad 函数中的所有内容都很好用。这是我的 viewDidLoad 函数

首先数组和变量是这样的

var imageList = ["usaflag","gerflag","franceflag","jpflag","gerflag"]

    var titleList = ["Croatian kuna","Hungarian forint","Congolese franc","Israeli Shekel","Nigerian naira"]

    var descriptionList = ["HRK","HUF","CDF","ILS","NGN"]





    var myCurrency:[String] = []
    var myValues:[Double] = []

    var aCheckEuro:Double = 0

    var resultCurrency:Double = 0

    var activeCurrency:Double = 0
    var zeroOriginActiveCurrency:Double = 0
    var oneDestActiveCurrency:Double = 0




    var currencySelected:String = ""
    var zeroOriginCurrencySelected:String = ""
    var oneDestCurrencySelected:String = ""

和 viewDidLoad 在这里

override func viewDidLoad() {
        super.viewDidLoad()


        let url = URL(string: "http://data.fixer.io/api/latest?access_key=....")


        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in


            if (error != nil)
            {
                print("ERROR")
            }
            else
            {

                if let content = data
                {
                    do
                    {
                        let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                        print(myJson)

                        if let rates = myJson["rates"] as? NSDictionary
                        {
                            for (key, value) in rates
                            {
                                self.myCurrency.append((key as! String))
                                self.myValues.append((value as? Double)!)
                            }
                            print(self.myCurrency)
                            print(self.myValues)
                        }

                    }
                    catch
                    {
                    }

                }

            }

        }
        task.resume()
        }

正如我所说的,直到这里一切正常。这些都在一个tableViewController. 问题出在这个函数上

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




        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

        // Configure the cell...

        cell.cellTitle.text = titleList[indexPath.row]
        cell.cellDescription.text = descriptionList[indexPath.row]

        cell.cellImageView.image = UIImage(named : imageList[indexPath.row])


        cell.currencyCount.text = myCurrency[indexPath.row] // here has fatal erroe

        return cell
    }

最后一行

 cell.currencyCount.text = myCurrency[indexPath.row]

有致命错误,我不知道如何解决它。我应该提到这currencyCount是一个标签。我真的很感激任何帮助。谢谢 。

用户2296278

您将创建一个这样的数组:

var myCurrency:[String] = []

第一次 TableView 加载时,您的数组为空

那里你会写这个代码:

    if myCurrency.isEmpty == false {

                cell.currencyCount.text = myCurrency[1]
            }
            else
            {
                self.tableView.reloadData()
            }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章