【发布时间】:2017-12-03 18:34:53
【问题描述】:
我正在尝试从 JSON 中为 UICollectionview 中的每顿饭设置部分。我只是做不到
{
"meals" : [
{
"id" : 279,
"unit" : "حبة",
"disable" : "0",
"created_at" : "2017-03-06 19:17:23",
"time" : "فوري",
"small" : "0",
"type" : "2",
"desc" : "",
"price" : "6",
"image" : "a4f730f447e753a995e342e12446ab1958bdb5c3136b6.jpg",
"family_id" : "115",
"updated_at" : "2017-03-06 19:17:23",
"deleted_at" : null,
"medium" : "0",
"large" : "0",
"name" : "ساندوتش فلافل كعك بالسمسم "
},
{
"id" : 280,
"unit" : "حبة",
"disable" : "0",
"created_at" : "2017-03-06 19:21:51",
"time" : "فوري",
"small" : "0",
"type" : "2",
"desc" : "",
"price" : "5",
"image" : "1c65581aa774203a4335bc6d93fe2b6358bdb6f3edb23.jpg",
"family_id" : "115",
"updated_at" : "2017-03-06 19:22:27",
"deleted_at" : null,
"medium" : "0",
"large" : "0",
"name" : "سندويش فلافل عربي اصناف "
},
type 是餐食的部分名称。每顿饭都会有一个部分 1 ,2 ,3,4
现在我像这样设置 UICollectionview 的行:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if meals.count > 0 {
self.collectionView.backgroundView = nil
return meals.count
}
let rect = CGRect(x: 0,
y: 0,
width: self.collectionView.bounds.size.width,
height: self.collectionView.bounds.size.height)
let noDataLabel: UILabel = UILabel(frame: rect)
noDataLabel.text = "تقدر تطلب اي شي من \(nameOfShop.text!)"
noDataLabel.textColor = UIColor.black
noDataLabel.textAlignment = NSTextAlignment.center
self.collectionView.backgroundView = noDataLabel
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mealsCell", for: indexPath) as! MealCollectionViewCell
let entry = meals[indexPath.row]
cell.mealName.text = entry.meal_name
cell.mealPrice.text = "\(entry.price) ريال"
cell.mealImage.hnk_setImage(from: URL(string: entry.Logo))
return cell
}
这是我从 API 获取数据的方式
let name = subJson["name"].stringValue
let price = subJson["price"].stringValue
let logo = subJson["image"].stringValue
let id = subJson["id"].stringValue
let logoString = "http://app.com/upload/img/\(logo)"
let info = Meals(meal_id: id, Logo: logoString, meal_name: name, price: price)
self.meals.append(info)
抱歉,代码太长了,我想我应该解释一下。
【问题讨论】:
-
到底是什么问题?什么不工作?
-
我想为餐点添加部分。部分名称是来自 json 的
type。每餐都附有type -
allShops是您的数据数组吗?那么最简单的方法是将其组织为一个数组数组,或者一个带有数组的字典(类型是每个数组的键)。这样你就可以直接在你的 dataSource 委托方法中查找每个部分的数组:而不是allShops.count然后你有allShops[section].count和而不是let shop = allShops[indexPath.row]你将有let shop = allShops[indexPath.section][indexPath.row] -
对不起,你能再检查一下我的问题吗?不是这个代码我复制了错误的代码......
标签: ios swift uicollectionview swift4