![](/img/trans.png)
[英]Swift: how to call json id from UIButton CollectionView cell
[英]How to return Max 10 Items in collectionview numberOfItemsInSection from JSON in Swift
我收到 Json 響應,如下所示
detailsDB = ProductDetailsData(dictionary: NSDictionary())
func serviceCall(){
let param = ["jsonrpc": "2.0",
"params": ["product_id" : productID
]] as [String : Any]
APIReqeustManager.sharedInstance.serviceCall(param: param, vc: self, url: getUrl(of: .product_details), header: header) {(responseData) in
self.detailsDB = ProductDetailsData(dictionary: responseData.dict as NSDictionary? ?? NSDictionary())
}
像這樣,我將 JSON 中的所有產品都顯示在 collectionview 中。這里我需要顯示從 JSON 到 collectionview 的最多 10 個產品
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return detailsDB?.result?.seller_products?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VerticalSliderCollectionCell", for: indexPath) as! VerticalSliderCollectionCell
let indexData = detailsDB?.result?.seller_products?[indexPath.item]
cell.imgProduct.getImage(strUrl: "\(productImagePath)\(indexData?.default_image?.image ?? "")")
cell.lblTitle.text = indexData?.product_by_language?.title
cell.lbRealPrice.text = indexData?.discount_price != "0.000" ? "\(indexData?.price ?? "") KWD" : ""
cell.lblDiscountedPrice.text = indexData?.discount_price != "0.000" ? "\(indexData?.discount_price ?? "") KWD" : "\(indexData?.price ?? "") KWD"
return cell
}
請幫我解決這個錯誤
試試這種方式:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return min(detailsDB?.count, 10) // 10 = max items
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.