如果计数等于0,如何将显示数据从API跳过到集合视图单元格中

时间:2019-06-17 17:13:36

标签: ios swift

我正在将来自API的数据解析为一个集合视图,其中有一个名为Carbon::now()的字段。我不想显示count为0的数据,我希望其他集合视图单元格相应地重新排列。

资产文件夹的屏幕截图: screenshot of assets folder

我的截图结果 result of my app when it starts

到目前为止,这是我的代码:

count

2 个答案:

答案 0 :(得分:0)

我认为您应该在这样的响应方法中添加一个条件,以消除count属性等于0的数据。

if resul_array_tmp_new.count > 0 {
    for i in modelResponse.store! {
        if i.count != 0 {
            self.storeData.append(i)
        }
    }
}

与类别列表相同。

if resul_array_tmp_new.count > 0 {
    for i in modelResponse.categories! {
        if i.count != 0 {
            self.categoryData.append(i)
        }
    }
}

答案 1 :(得分:0)

您可以在

中添加条件
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if selectedBtnIndex == 1{
    return (categoryData.count > 0) ? categoryData.count : 0
}else {
    return (storeData.count > 0) ? storeData.count : 0
}}

并更改cellForRowAt IndexPath

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if selectedBtnIndex == 1{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCell1", for: indexPath) as! CategoryCell1

    if categoryData.count > indexPath.row {
        let dict = categoryData[indexPath.row]

        if let catName = dict.name, catName.count != 0 {
            cell.categoryName.text = catName
        }

        if let catOffersCount = dict.count {
            if catOffersCount <= 1 {
                cell.catOfferCount.text = "\(catOffersCount)"+" "+"Offer"
                cell.categoryImage.image = UIImage(named: "tickets")
            }else {
                cell.catOfferCount.text = "\(catOffersCount)"+" "+"Offers"
            }
        }

        if dict.slug == "mens" {
            cell.categoryImage.image = UIImage(named: "MENS")
        } else if dict.slug == "electronics" {
            cell.categoryImage.image = UIImage(named: "ELECTRONICS")
        }else if dict.slug == "beauty" {
            cell.categoryImage.image = UIImage(named: "BEAUTY")
        }else if dict.slug == "fashion" {
            cell.categoryImage.image = UIImage(named: "FASHION")
        }else if dict.slug == "kids-clothing" {
            cell.categoryImage.image = UIImage(named: "KIDS FASHION")
        }else if dict.slug == "travel" {
            cell.categoryImage.image = UIImage(named: "TRAVEL")
        }else if dict.slug == "womens-apparels" {
            cell.categoryImage.image = UIImage(named: "WOMENS APPARELS")
        }else if dict.slug == "eyewear" {
            cell.categoryImage.image = UIImage(named: "EYEWEAR")
        }else {
            cell.categoryImage.image = UIImage(named: "tickets")
        }


        cell.btn_click.tag = indexPath.row
        cell.btn_click.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
        cell.btn_click.setImage(#imageLiteral(resourceName: "image_checked"), for: .selected)
        cell.btn_click.addTarget(self, action: #selector(self.click_Category), for: .touchUpInside)
    }

    return cell
}else {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoresCell", for: indexPath) as! StoresCell
    if storeData.count > indexPath.row {
        let dict = storeData[indexPath.row]

        if let storeName = dict.name, storeName.count != 0 {
            cell.storeName.text = storeName
        }

        if let storeOfferCount = dict.count {
            cell.storeOfferCount.text = "\(storeOfferCount)"+" "+"Offers"
        }

        cell.store_btn_click.tag = indexPath.row
        cell.store_btn_click.setImage(#imageLiteral(resourceName: "image_unchecked"), for: .normal)
        cell.store_btn_click.setImage(#imageLiteral(resourceName: "image_checked"), for: .selected)
        cell.store_btn_click.addTarget(self, action: #selector(self.click_store), for: .touchUpInside)
    }

    return cell
}}