Reputation: 666
I have an api call response where i will get the list of items as two format.
"items" : [
{
"menu_code" : "NDS",
"name" : "Monday"
},
{
"menu_code" : "NDN",
"name" : "Tuesday"
}
]
format 2 is here :
"items" : [
{
"unit" : "Nos",
"name" : "Chapathi\/Pulkas",
"quantity" : 2
},
{
"unit" : "Cup",
"name" : "Palya\/Curry",
"quantity" : 1
}
]
Now i have one label in my collection view. So based the response in my label i needs to show like below example :
name - quantity unit ,
name - quantity unit,
name - quantity unit ...etc
based on the count coming from response.
Another format :
name - menu_code,
name - menu_code,
name - menu_code ..etc
based on the count coming from response.
My model class :
struct Item : Codable {
let unit : String?
let name : String?
let quantity : Int?
let menuCode : String?
}
my collection view :
var names:[String] = []
var qty:[Int] = []
var unit:[String] = []
var menuCode:[String] = []
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as! HomeCollectionCell
cell.productName.text = self.allCategory[indexPath.item].menuName
let itemsData = self.allCategory[indexPath.row].items
print(itemsData)
for dt in itemsData {
// print(dt)
let nam = dt.name
let unt = dt.unit
let mCode = dt.menuCode
let qtys = dt.quantity
names.append(nam ?? "")
unit.append(unt ?? "" )
qty.append(qtys ?? 0)
menuCode.append(mCode ?? "" )
// cell.ProductsubLabel.text = itemsData
}
return cell
}
So i have created an array but not sure how to append to label.And also i am not aware to difference the two format and to show in the label.Any help on this.
Thanks in advance !
Update :
third new format :
"items" : [
{
"unit" : "Nos",
"product_name" : "Chapathi\/Pulkas",
"quantity" : 2
},
{
"unit" : "Cup",
"product_name" : "Palya\/Curry",
"quantity" : 1
}
]
Upvotes: 0
Views: 60
Reputation: 285082
A quite efficient solution is to add a description
property in the struct which returns the appropriate data. If unit
does not exist return the menuCode
information otherwise quantity
and unit
struct Item : Codable {
let unit : String?
let name : String?
let productName : String?
let quantity : Int?
let menuCode : String?
var description : String {
let name = self.name ?? self.productName ?? "n/a"
if unit == nil {
return "\(name) - \(menuCode!)"
} else {
return "\(name) - \(quantity!) \(unit!)"
}
}
}
In cellForItemAt
map the items
to their descriptions and join the array by comma.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as! HomeCollectionCell
let category = self.allCategory[indexPath.item]
cell.productName.text = category.menuName
let itemsData = category.items
let subData = itemsData.map {$0.description}.joined(separator: ", ")
cell.ProductsubLabel.text = subData
print(subData)
return cell
}
And delete the four ugly arrays above cellForItemAt
Upvotes: 1