微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想在 Swift 中为 UICollectionView 中的特定部分创建粘性标题

如何解决我想在 Swift 中为 UICollectionView 中的特定部分创建粘性标题

我使用了包含 3 个不同部分的集合视图,其中第一部分显示横幅,第二部分显示子类别,第三部分显示项目/产品,现在我想要的是当我滚动整个视图时,第二部分应该作为滚动时的粘性标题。 我已经做了所有可以做的事情,但离解决问题还差得很远 请帮帮我

func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
    switch section {
    case 0: return 1
    case 1: return subCategories.count 
    case 2: return selectedSubCategoryID == -1 ? products.count : productsSub.count
    default: return 0
    }
}

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    switch indexPath.section {
    case 0:
        
        let cell = collectionView.dequeueReusableCell(ofType: ShopBannerCell.self,for: indexPath)
        
        cell.shoplogoImageView.loadImage(shop.logo)
        cell.shoplogoImageView.layer.borderColor = UIColor.lightGray.cgColor
        cell.shoplogoImageView.layer.borderWidth = 1
        
        cell.shopBannerImageView.loadImage(details?.sliders?.first)
        
        cell.images = details?.sliders ?? []
        
        cell.shopNameLabel.text = shop.name
        //            cell.lcoationButton.setTitle(details?.address,for: .normal)
        
        cell.showSideMenu = { [weak self] sender in
            if let navVC = self?.navigationController as? NavigationController {
                navVC.showSideMenu()
            }
        }
        if currentLanguage == "en" {
            cell.backArrowBtn.setimage(#imageLiteral(resourceName: "left_arrow"),for: .normal)
        } else
        {
            cell.backArrowBtn.setimage(#imageLiteral(resourceName: "right-arrow"),for: .normal)
        }
        cell.backButton = { [weak self] sender in
            self?.navigationController?.popViewController(animated: true)
        }
        cell.showSearch = { [weak self] sender in
            NotificationCenter.default.post(name: .navigatetoShopVC,object: self)
        }
        
        cell.showShopVC = { [weak self] sender in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        
        cell.showFilterVC = { [weak self] sender in
            let productFilterVC = ProductFilterVC()
            productFilterVC.modalPresentationStyle = .overFullScreen
            
            productFilterVC.showShopVC = { [weak self] in
                self?.navigationController?
                    .popToRootViewController(animated: true)
            }
            
            // variant_product
            
            productFilterVC.applyFilters = { [weak self] filter in
                
                if let priceRange = filter.priceRange,!priceRange.isEmpty,priceRange != 0...0 {
                    if self?.selectedSubCategoryID == -1 {
                        self?.products = self?.products.filter { priceRange.contains(Int($0.basePrice ?? 0))} ?? []
                    } else {
                        self?.productsSub = self?.productsSub.filter { priceRange.contains(Int($0.unitPrice ?? "0") ?? 0)} ?? []
                    }
                }
                
                if filter.sortOrder != -1 {
                    if self?.selectedSubCategoryID == -1 {
                        self?.products.sort(by: { item1,item2 -> Bool in
                            switch filter.sortOrder {
                            case 0: return item1.basePrice ?? 0 < item2.basePrice ?? 0
                            case 1: return item1.basePrice ?? 0 > item2.basePrice ?? 0
                            case 2: return item1.discount ?? 0 < item2.discount ?? 0
                            default: return true
                            }
                        })
                    } else {
                        self?.productsSub.sort(by: { item1,item2 -> Bool in
                            switch filter.sortOrder {
                            case 0: return Double(item1.unitPrice ?? "") ?? 0 < Double(item2.unitPrice ?? "") ?? 0
                            case 1: return Double(item1.unitPrice ?? "") ?? 0 > Double(item2.unitPrice ?? "") ?? 0
                            case 2: return Double(item1.discount ?? "") ?? 0 < Double(item2.discount ?? "") ?? 0
                            default: return true
                            }
                        })
                    }
                }
                
                self?.collectionView.reloadSections(IndexSet(integer: 2))
            }
            
            self?.present(productFilterVC,animated: true)
        }
        
        cell.gridButton.isSelected = self.selectedLayout == .grid
        cell.listButton.isSelected = self.selectedLayout == .list
        
        cell.listlayout = { [weak self] sender in
            self?.selectedLayout = .list
            self?.collectionView.collectionViewLayout.invalidateLayout()
            self?.collectionView.reloadData()
        }
        
        cell.gridLayout = { [weak self] sender in
            self?.selectedLayout = .grid
            self?.collectionView.collectionViewLayout.invalidateLayout()
            self?.collectionView.reloadData()
        }
        
        return cell
        
    case 1:
        
        let cell = collectionView.dequeueReusableCell(ofType: SubCategoryCell.self,for: indexPath)
        
        let subCategory = subCategories[indexPath.row]
        
        if subCategory.id == selectedSubCategoryID {
            cell.button.isSelected = true
            cell.button.backgroundColor = .accentColor
        } else {
            cell.button.isSelected = false
            cell.button.backgroundColor = .clear
        }
        
        if currentLanguage == "en" {
            cell.button.setTitle(subCategory.name?.capitalized,for: .normal)
        } else
        {
            cell.button.setTitle(subCategory.ar_name?.capitalized,for: .normal)
        }
        
        
        cell.onPress = { [weak self] sender in
            
            guard let self = self else { return }
            
            self.selectedSubCategoryID = self.subCategories[indexPath.row].id ?? 1
            
        }
        
        return cell
        
    case 2:
        
        if selectedLayout == .grid {
            
            let cell = collectionView.dequeueReusableCell(ofType: ProductCell.self,for: indexPath)
            
            //                cell.addToCartButton.setTitle("Add to Cart".localized(),for: .normal)
            
            let image = selectedSubCategoryID == -1 ? products[indexPath.row].photos.first : productsSub[indexPath.row].photos?.first
            cell.productimageView.loadImage(image as? String)
            if currentLanguage == "en"{
                cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].name : productsSub[indexPath.row].name
            } else {
                cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].arName : productsSub[indexPath.row].arName
            }
            
            
            cell.pricingLabel.text = "KD ".localized() + "\(products[indexPath.row].basediscountedPrice ?? "")"
            
            if products[indexPath.row].basePrice == Double(products[indexPath.row].basediscountedPrice ?? ""){
                cell.discountView.isHidden = true
                cell.pricingLabel.textAlignment = .center
                cell.pricingLabel.textColor = UIColor.black
                
            }
            else
            {
                cell.discountView.isHidden = false
                cell.discountedLBL.text = "KD " + (String(products[indexPath.row].basePrice ?? 0)) + "0"
                cell.pricingLabel.textColor = UIColor.red
            }
            
            //                let price = selectedSubCategoryID == -1 ? "\(products[indexPath.row].basePrice ?? 0)" : productsSub[indexPath.row].unitPrice ?? ""
            
            
            
            return cell
            
        } else {
            
            let cell = collectionView.dequeueReusableCell(ofType: ProductListCell.self,for: .normal)
            
            
            let image = selectedSubCategoryID == -1 ? products[indexPath.row].photos.first : productsSub[indexPath.row].photos?.first
            cell.productimageView.loadImage(image as? String)
            cell.productimageView.layer.borderColor = UIColor.lightGray.cgColor
            cell.productimageView.layer.borderWidth = 1
            cell.pricingLabel.text = "KD ".localized() + "\(products[indexPath.row].basediscountedPrice ?? "")" 
            
            
            if products[indexPath.row].basePrice  == Double(products[indexPath.row].basediscountedPrice ?? ""){
                cell.discountView.isHidden = true
                cell.pricingLabel.textAlignment = .center
                cell.pricingLabel.textColor = UIColor.black
            }
            else
            {
                cell.discountView.isHidden = false
                cell.discountLbl.text = "KD " + (String(products[indexPath.row].basePrice ?? 0)) + "0"
                cell.pricingLabel.textColor = UIColor.red
            }
            
            if currentLanguage == "en"{
                cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].name : productsSub[indexPath.row].name
            } else {
                cell.productNameLabel.text = selectedSubCategoryID == -1 ? products[indexPath.row].arName : productsSub[indexPath.row].arName
            }
           
            
            //                let price = selectedSubCategoryID == -1 ? "\(products[indexPath.row].basePrice ?? 0)" : productsSub[indexPath.row].unitPrice ?? ""
            
            
            let productID = selectedSubCategoryID == -1 ? "\(products[indexPath.row].id ?? 0)" :  "\(productsSub[indexPath.row].id ?? "")"
            
            cell.onAddToCart = { [weak self] _ in
                
                if UserRepository.shared.userName == "GUEST" {
                    self?.promptForGuestUser()
                    return
                }
                
                self?.addToCartAPI(product: "\(productID)")
            }
            
            return cell
            
        }
        
        
    default:
        return collectionView.dequeueReusableCell(ofType: SubCategoryCell.self,for: indexPath)
    }
    
}

func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath,animated: true)
    
    switch indexPath.section {
    case 2:
        
        let product = products[indexPath.row]
        let vc = ProductDetailsVC(id: "\(product.id ?? 0)",subCategoryID: product.subcategoryID ?? "",sellerID: product.sellerID ?? "")
        navigationController?.pushViewController(vc,animated: true)
        
    default: break
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。