Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中将数组中的数据分解为表中的标签?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在Swift中将数组中的数据分解为表中的标签?

Ios 如何在Swift中将数组中的数据分解为表中的标签?,ios,swift,uitableview,Ios,Swift,Uitableview,我从网站上获取JSON。我在模型中列出了数据。从模型中,我将数据加载到数组“arrayOfMoney”中。现在如何把它放在桌子上的标签上 这是一个模型: 导入基础 结构值:可解码{ 美元、欧元、卢克:类型货币 } 结构类型货币:可解码{ 让字母代码:字符串 let name:String 让我们倒数:双倍 } 创建属性: var arrayOfImage=[“美元”、“欧元”、“新加坡元”、“比亚尼”、“印尼国家电力公司”、“土耳其卢比”、“KZT”] var arrayOfMoney=[Va

我从网站上获取JSON。我在模型中列出了数据。从模型中,我将数据加载到数组“arrayOfMoney”中。现在如何把它放在桌子上的标签上

这是一个模型:

<代码>导入基础 结构值:可解码{ 美元、欧元、卢克:类型货币 } 结构类型货币:可解码{ 让字母代码:字符串 let name:String 让我们倒数:双倍 } 创建属性:

var arrayOfImage=[“美元”、“欧元”、“新加坡元”、“比亚尼”、“印尼国家电力公司”、“土耳其卢比”、“KZT”]
var arrayOfMoney=[Valute](){
迪塞特{
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
接下来,我们获取数据并填充数组“arrayOfMoney”:

private func fetchRequest(){
guard let url=url(字符串:Contents.urlString)else{return}
URLSession.shared.dataTask(带:url){(数据、响应、错误)在
guard let data=data,error==nil else{return}
做{
让parseJson=try JSONDecoder().decode(Valute.self,from:data)
self.arrayOfMoney.insert(parseJson,位于:0)
}捕捉错误{
打印(错误。本地化描述)
}
}1.简历()
}
现在我以这种方式插入数据:

扩展TableViewController{
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回arrayOfMoney.count
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“cell”,for:indexPath)作为!CustomizeCell
configureCell(cell:cell,for:indexath)
返回单元
}
private func configureCell(单元格:CustomizeCell,对于indexPath:indexPath){
让currency=arrayOfMoney[indexPath.row]
cell.charcodelab?.text=currency.usd.alphaCode
cell.namelab?.text=currency.usd.name
cell.valueTextField.text=“\(NSString(格式:%.2f”,货币.usd.inverseRate))”
cell.imageViewPic.image=UIImage(名称:arrayOfImage[indexPath.row])
}
但是我需要插入所有的货币,我不知道如何动态地这样做


感谢您根据您必须声明的代码作出的回复

var arrayOfMoney = [TypeCurrency]()
并填充它

 let parseJson = try JSONDecoder().decode(Valute.self, from: data)
 self.arrayOfMoney = [parseJson.usd, parseJson.eur, parseJson.lkr]
 DispatchQueue.main.async {
    self.tableView.reloadData()
 }
然后将
configureCell
更改为

private func configureCell(cell: CustomizeCell, for indexPath: IndexPath) {
    let currency = arrayOfMoney[indexPath.row]
    cell.charCodeLabel?.text = currency.alphaCode
    cell.nameLabel?.text = currency.name
    cell.valueTextField.text = String(format:"%.2f", currency.inverseRate) // округляем до двух знаков после запятой
    cell.imageViewPic.image = UIImage(named: arrayOfImage[indexPath.row])
}

您应该创建一个对象数组(详细信息显示在表单元格中)。在
tableView
的下面的数据源方法中使用此数组。您将获得具有相应行的IndexPath。您可以使用该数组访问数组中的相应数据,以在单元格中显示它

func configureCell(cell: CustomizeCell, for indexPath: IndexPath) {}

我有一个声明的数组,只有它的类型“Valute”
Valute
是单个对象,即使您将其声明为数组。您需要将
TypeCurrency
属性作为数组加入,如我的回答中所述。它不符合您的方法。写入了一个类型错误。我在此处添加了一些代码,我更新了答案。请严格按照我的建议进行操作。非常感谢!谢谢g工作了。现在我会考虑它是如何工作的。创建了数组。但我不能将其中的所有数据填充到一个表中。我有50种货币,我必须为表中的每种货币创建一个手动单元格,但我想动态地创建。你也可以动态地做,你需要为每行创建一个数组对象。