I have tableView with a cell of multiple labels. Having cell-like, set-1 variableName1, variablevalue1 & set-2 variableName2, variablevalue2. Here all the data will get from JSON responses including the title label.
I have stored all values to model and showing some data in tableView. but I could not able to show the multiple label values alone.
Here is my JSON structure for multiple labels:
"_embedded": {
"variable": [
{
"_links": {
"self": {
"href": ""
}
},
"_embedded": null,
"name": "startedBy",
"value": "Preethi",
"type": "String",
"valueInfo": {}
},
{
"_links": {
"self": {
"href": ""
}
},
"_embedded": null,
"name": "LoanAmount",
"value": "1500000",
"type": "String",
"valueInfo": {}
}
]
},
Here I am trying to show the value from the variable array -> name & value.
Tried so far like this:
for val in (t._embedded?.variable)!{
print("val name", val.name ?? "")
print("val name", val.value ?? "")
if val.name == val.name {
cell.variableName1.text = val.name
cell.variablevalue1.text = val.value
}
if val.value == val.value {
cell.variableName2.text = val.name
cell.variablevalue2.text = val.value
}
}
here the image of the tableView cell:
Any help much appreciated pls…
3
Answers
You can use TableView in CollectionView
If you compare val.name == val.name it always returns true so you need to modify like this val.name == "startedBy" and for value val.value == "1500000" to display the first item and second item of the json.
Anyway where do you call the for loop?
I think you want to display each item of the json in a unique cell so you need to parse the json into a dictionary and use it in the "cellForItemAt" function of CollectionViewDataSource
There is little information on what you really want but maybe this can help.
Case 1
I feel like your json may not be properly defined. If it is, then it seems to say that you will only have one set of two variables, then you wouldn’t need a
UITableView
because there would be only one cell, that way you could accomplish that by just adding oneUIView
to your current view.If you:
On
cellForRow(at:)
you canCase 2
If your json is properly defined but you will have more than two variables and you want to show two per row then I would suggest you used
UICollectionViewCell
and have your cells take half the screen in size each and in each one you show the value of the proper index. Something like this:Case 3
If none of these is still the case you want solved please let us know so that we can better help you.