问题描述
我使用了包含 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 (将#修改为@)