代码之家  ›  专栏  ›  技术社区  ›  PvDev

如何在iOSswift中设置特定索引的集合视图单元?

  •  0
  • PvDev  · 技术社区  · 6 年前

    在配置文件屏幕中,从firestore中检索用户名、电子邮件ID、汽车类别的数据。这里的汽车类别是紧凑型、小型、中型和全尺寸。并将carCategory参数值存储为字符串(即:carCategory:“1”,这意味着 car category is “0” then its compact car if car category value is “1” then its small.)。

    在用户更新页面中,汽车类别是一个集合视图流布局是旋转视图
    如果汽车类别单元格索引路径值为1,则显示单元格详细信息而不滚动。

    以下是我例外的屏幕截图:

    我的问题是如何在集合视图单元中加载特定的单元索引。

    这是我迄今为止尝试过的代码:

    **collection view and carousel view**
    
    var car=[string]()
    var carcategory=[“紧凑型”、“小型”、“中型”、“全型”、“厢式货车/皮卡车”]
    var carCategoryImage=[“CompactCar”,“SmallCar”,“MediumCar”,“FullCar”,“VanPickup”]
    var carcategorymeter=[“3.5-4.5 m”,“2.5-3.5 m”,“4-5 m”,“5-5.5 m”,“5.5-6.5 m”]
    
    var carcategorylabel:uilabel?
    变量CarmetersLabel:uiLabel?
    var carCategoryImageLabel:uiImageView?
    var currentpagevalue:字符串?
    var当前单元格:int?
    
    func集合视图(uCollectionView:uiCollectionView,NumberOfitemsInSection:int)->int{
    
    返回carcategory.count
    
    }
    
    func集合视图(uCollectionView:uiCollectionView,cellForItemAt indexPath:indexPath)->uiCollectionViewCell{
    让cell=collectionview.dequeuerusablecell(带有reuseIdentifier:“cell”,用于:indexPath)作为!汽车收藏设置收藏查看单元
    
    cell.carName.text=carCategory[indexPath.row]
    打印(“carCategoryIndex\(carCategory)”)
    cell.carImage.image=uiImage(名称:carCategoryImage[indexPath.row])
    cell.carmeters.text=carCategoryMeter[indexPath.row]
    
    
    
    
    返回单元格
    }
    
    
    fileprivate var pagesize:cgsize{
    让layout=self.carcollection.collectionviewlayout作为!上转盘下布局
    var pagesize=layout.itemsize
    如果layout.scrollDirection=.horizontal{
    pagesize.width+=布局。最小行距
    }其他{
    pagesize.height+=布局。最小行距
    }
    返回页面大小
    }
    
    func scrollview双端减速(u scrollview:uiscrollview){
    让layout=carcollection.collectionviewlayout作为!上转盘下布局
    让pageside=(layout.scrollDirection==.horizontal)?self.pagesize.width:self.pagesize.height
    设为offset=(layout.scrolldirection==.horizontal)?scrollview.contentoffset.x:scrollview.contentoffset.y
    当前页=int(楼层((偏移-页边/2)/页边)+1)
    打印(“当前页::::\(当前页)”)
    
    
    }
    
    fileprivate var当前页:int!{
    迪塞特{
    
    当前页=self.currentCell
    打印(“当前页::::\(当前页)”)
    
    让character=self.carcategory[self.currentpage!]
    打印(“字符::::\(字符)”)
    
    }
    }
    
    func loadUserData()。{
    
    
    中的api.user.observecurrentuser(user)
    
    如果self.username.text!=零{
    self.username.text=用户名
    print(“用户名::\(字符串(描述:user.username))”)
    }
    如果让photourlstring=user.profileimageurl{
    让photourl=url(字符串:photourl string)
    self.profileimage.sd_setimage(带:photourl)
    }
    
    如果self.email.text!=零{
    
    self.email.text=用户.email
    
    
    }
    
    
    
    
    
    切换user.carcategory{
    
    案例“0”:
    
    Self.Carcategorylabel?.text=“紧凑型”
    Self.CarmetersLabel?.text=“3.5-4.5米”
    self.carCategoryImageLabel?.image=uiimage(命名为“CompactCar”)
    self.currentCell=0
    print(“currentCell::\(string(describing:self.currentCell))”)
    self.carcollection.reloaddata()。
    
    案例“1”:
    
    Self.Carcategorylabel?.text=“小”
    Self.CarmetersLabel?.text=“2.5-3.5米”
    self.carCategoryImageLabel?.image=uiimage(命名为:“smallcar”)
    Self.Carcategorylabel?.text=“1”
    self.currentCell=1
    print(“currentCell::\(string(describing:self.currentCell))”)
    self.carcollection.reloaddata()。
    
    
    案例“2”:
    
    Self.Carcategorylabel?.text=“中型”
    Self.CarmetersLabel?.text=“4-5米”
    self.carCategoryImageLabel?.image=uiimage(命名为“mediumcar”)
    self.currentCell=2个
    print(“currentCell::\(string(describing:self.currentCell))”)
    self.carcollection.reloaddata()。
    案例“3”:
    
    Self.Carcategorylabel?.text=“满”
    Self.CarmetersLabel?.text=“5-5.5米”
    self.carCategoryImageLabel?.image=uiimage(命名为“fullcar”)
    self.currentcell=3个
    print(“currentCell::\(string(describing:self.currentCell))”)
    self.carcollection.reloaddata()。
    
    案例“4”:
    
    Self.Carcategorylabel?.text=“货车/皮卡车”
    Self.CarmetersLabel?.text=“5.5-6.5米”
    self.carCategoryImageLabel?.image=uiimage(命名为:“vanpickup”)
    self.currentCell=4个
    print(“currentCell::\(string(describing:self.currentCell))”)
    self.carcollection.reloaddata()。
    
    
    违约:
    
    Self.Carcategorylabel?.text=“”
    Self.CarmetersLabel?.text=“”
    self.carCategoryImageLabel?.image=uiimage(名称:“”)
    
    打破
    }
    
    
    
    
    
    }
    
    
    }//加载用户数据
    汽车类别是“0”那么它的小型汽车如果汽车类别值“1”然后是小的)。

    在用户更新页面中,汽车类别是集合视图流布局是旋转视图
    如果汽车类别单元格索引路径值为1,则显示单元格详细信息而不滚动。

    以下是我的截图:enter image description here

    我的问题是如何在集合视图单元中加载特定的单元索引。

    这是我迄今为止尝试的代码:

       **collection view and carousel view**
    
        var car = [String]()
    var carCategory = [ "Compact","small", "Midsize", "Full", "Van/Pick-up" ]
    var carCategoryImage = ["compactCar", "smallCar", "mediumCar", "fullCar", "vanPickup"]
    var carCategoryMeter = ["3.5 - 4.5m", "2.5 - 3.5m", "4 - 5m", "5 - 5.5m", "5.5 - 6.5m"]
    
    var carCategoryLabel: UILabel?
    var carMetersLabel: UILabel?
    var carCategoryImageLabel: UIImageView?
    var currentPageValue: String?
    var currentCell: Int?
    
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
        return carCategory.count
    
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! carCollectionSettingCollectionViewCell
    
        cell.carName.text = carCategory[indexPath.row]
        print("carcategoryIndex\(carCategory)")
        cell.carImage.image = UIImage(named: carCategoryImage[indexPath.row])
        cell.carMeters.text = carCategoryMeter[indexPath.row]
    
    
    
    
        return cell
    }
    
    
    fileprivate var pageSize: CGSize {
        let layout = self.carCollection.collectionViewLayout as! UPCarouselFlowLayout
        var pageSize = layout.itemSize
        if layout.scrollDirection == .horizontal {
            pageSize.width += layout.minimumLineSpacing
        } else {
            pageSize.height += layout.minimumLineSpacing
        }
        return pageSize
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let layout = carCollection.collectionViewLayout as! UPCarouselFlowLayout
        let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
        let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
        currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
        print("currentpage::::\(currentPage)")
    
    
    }
    
    fileprivate var currentPage: Int! {
        didSet {
    
            currentPage = self.currentCell
            print("currentapge::::\(currentPage)")
    
            let character = self.carCategory[self.currentPage!]
            print("character::::\(character)")
    
        }
    }
    
       func loadUserData(){
    
    
        API.User.observeCurrentUser { (user) in
    
            if self.userName.text != nil {
                self.userName.text = user.username
                print("username:::\(String(describing: user.username))")
            }
            if let photoUrlString = user.profileImageURL {
                let photoUrl = URL(string: photoUrlString)
                self.profileImage.sd_setImage(with: photoUrl)
            }
    
            if self.email.text != nil {
    
                self.email.text = user.email
    
    
            }
    
    
    
    
    
                switch user.carCategory {
    
                case "0":
    
                    self.carCategoryLabel?.text = "Compact"
                    self.carMetersLabel?.text = "3.5 - 4.5m"
                    self.carCategoryImageLabel?.image = UIImage(named: "compactCar")
                    self.currentCell = 0
                    print("currentCell:::\(String(describing: self.currentCell))")
                    self.carCollection.reloadData()
    
                case "1":
    
                    self.carCategoryLabel?.text = "Small"
                    self.carMetersLabel?.text = "2.5 - 3.5m"
                    self.carCategoryImageLabel?.image = UIImage(named: "smallCar")
                    self.carCategoryLabel?.text = "1"
                    self.currentCell = 1
                    print("currentCell:::\(String(describing: self.currentCell))")
                    self.carCollection.reloadData()
    
    
                case "2":
    
                    self.carCategoryLabel?.text = "Midsize"
                    self.carMetersLabel?.text = "4 - 5m"
                    self.carCategoryImageLabel?.image = UIImage(named: "mediumCar")
                    self.currentCell = 2
                    print("currentCell:::\(String(describing: self.currentCell))")
                    self.carCollection.reloadData()
                case "3":
    
                    self.carCategoryLabel?.text = "Full"
                    self.carMetersLabel?.text = "5 - 5.5m"
                    self.carCategoryImageLabel?.image = UIImage(named: "fullCar")
                    self.currentCell = 3
                    print("currentCell:::\(String(describing: self.currentCell))")
                    self.carCollection.reloadData()
    
                case "4":
    
                    self.carCategoryLabel?.text = "Van/Pick-up"
                    self.carMetersLabel?.text = "5.5 - 6.5m"
                    self.carCategoryImageLabel?.image = UIImage(named: "vanPickup")
                    self.currentCell = 4
                    print("currentCell:::\(String(describing: self.currentCell))")
                    self.carCollection.reloadData()
    
    
                default:
    
                    self.carCategoryLabel?.text = ""
                    self.carMetersLabel?.text = ""
                    self.carCategoryImageLabel?.image = UIImage(named: "")
    
                    break
                }
    
    
    
    
    
        }
    
    
    }//loaduserData
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Starsky    6 年前

    这是如何做到的:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView.selectItem(at: [0, currentCell], animated: false, scrollPosition: .centeredHorizontally)
        }
    

    当您加载视图控制器时,我假设您设置了当前单元格?我看不到你的原件 viewDidLoad() viewDidAppear() 功能。如果您从以前的viewcontroller实例化这个viewcontroller,那么从那里设置您的currentCell。如果您使用此视图控制器,请在 prepareForSegue 方法。

        2
  •  1
  •   Rashed    6 年前

    下面的代码可能对您在这个场景中有所帮助

    // MARK: - Collectionview delegates
        extension FaqcategoriesViewController : UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return (Global.shared.FAQsCategoriesList?.list.count)!
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "faqcategoriesCollectioncell",
                                                          for: indexPath) as! FAQCategoryCollectionViewCell
            cell.configure(data: (Global.shared.FAQsCategoriesList?.list[indexPath.row])!)
            return cell
        }
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
            let storyBoard = Utilities.getStoryboard(name: SBMEMBERSANDCOMMISSIONS)
            let nextView  = storyBoard.instantiateViewController(withIdentifier: "faq") as! FAQViewController
            nextView.id = indexPath.row
            self.navigationController?.pushViewController(nextView, animated: false)
        }
    
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {
            let collectionViewSize = collectionView.frame.size.width
            print(self.view.frame.width)
            print(collectionView.frame.size.width)
            print( "width: \(collectionViewSize/2 - 20) height: \(collectionView.frame.size.height/3.3)")
            return CGSize(width: collectionViewSize/2 - 5, height: collectionView.frame.size.height/3.3)
        }
    
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return 1.0
        }
    
        func collectionView(_ collectionView: UICollectionView, layout
            collectionViewLayout: UICollectionViewLayout,
                            minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 1.0
        }
    
    }