我有一個UICollectionView,其中的單元格是從JSON響應數據填充的。目前這些單元格按類別填充,一切運行順利。根據JSON swift中的參數自動排序UICollectionViewCell
我的問題是,單元格是按字母順序排序的,它們是如何在JSON文件中排序的,但我希望按照每個類別包含的「喜歡計數」參數對它們進行排序。
到目前爲止,我已經找到了如何啓用用戶拖動和重新排列單元格,但我想實施一個「自動排序」基於每個類別的「喜歡」的數量降序。
我很感激任何幫助。
編輯 - 使用Alamofire進行API調用和Swiftyjson解析。
Alamofire.request(.GET, "mydomain").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar.arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.collectionView.reloadData()
}
}
}
使細胞
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HomeCollectionViewCell
var dict = arrRes[indexPath.row]
cell.catLabel.text = dict["name"] as? String
let likes = dict["likes count"] as? Int
cell.layoutIfNeeded()
return cell
}
讓我們看看你的反應/模型類和代碼你用來做API調用之後。 –
對dataSource和reloadData進行排序(如果需要)。 – Larme
我已更新我的問題以包含我正在使用的代碼。我可以儘可能用Swiftyjson返回計數數組,我只是不確定如何將這些信息用於排序目的。如果這太簡單的請求,我仍然是一個新手,因此非常抱歉。 –