简体   繁体   中英

Way to dequeue multiple cells in UIcollectionview

based on the screen shot, I will have a big collectionview to contain few cells (with colors). All cell will display only one time in the view except for the green one.The green one will display an array of users.

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

        if indexPath.item == 0{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topfeatureCellIndent, for: indexPath) as! topFeatureCell
            //configure if needed
            return cell
        }else if indexPath.item == 1{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! featureUserContainerViewCell
            cell.featureUsers = featureUser
            cell.selectUserdelegate = self
            return cell
        }else if indexPath.item == 2{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ticketLabelIdent, for: indexPath) as! ticketLabelCell
            return cell
        } else if indexPath.item == 3{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: whosgoingIdent, for: indexPath) as! whoGoingCell
            cell.config(withTimer: timeleft)
            return cell
        }
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: allUserCellIdent, for: indexPath) as! allUserCell
            let index = indexPath.item - 4
            let user = allPartyUserArr![index]
            cell.config(withUser: user)
            return cell

The way I need to display the last cell is by implement the code above but I think its not correct, because what if I want to add in other cells after displaying all the cells, is there any better way to dequeue the cell properly?

截图

I would suggest you to use 2 Sections in UICollectionView. keep all the one-time visible cells in section 0 and the cells which will represent array for users in section 1

This is how you can set number of sections

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}

To set Header of each section you can implement below functions and set any size for your header. CGSizeMake(0, 0) will hide the Header

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width : 0, height : 0)  // Header size
}

then number of items in each section

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
 if section == 0 {
        return 4
    }
    else {
        users.count
    }
//return (section == 0) ? 4 : users.count
}

to display cell

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


 if indexPath.section == 0 {
 // Based on Your implementation
          if indexPath.item == 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topfeatureCellIndent, for: indexPath) as! topFeatureCell
        //configure if needed
        return cell
    }else if indexPath.item == 1{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! featureUserContainerViewCell
        cell.featureUsers = featureUser
        cell.selectUserdelegate = self
        return cell
    }else if indexPath.item == 2{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ticketLabelIdent, for: indexPath) as! ticketLabelCell
        return cell
    } else if indexPath.item == 3{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: whosgoingIdent, for: indexPath) as! whoGoingCell
        cell.config(withTimer: timeleft)
        return cell
    } else {
     return UICollectionViewCell()
    }

 }else{
         //make sure the identifier of your cell for second section
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: allUserCellIdent, for: indexPath) as! allUserCell
       // populate your user cell here
        return cell
  }

}

You can set the number of sections in CollectionView like this :

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 5
}

Set number of items in each section :

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    switch section {
    case 0:
        return 1
    default:
        return 3
    }
}

Number of items you can set according to the section .

and for cells in each section :

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

    switch indexPath.section {
    case 0:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topfeatureCellIndent, for: indexPath) as! topFeatureCell
        //configure if needed
        return cell
    case 1:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userCellIdent, for: indexPath) as! featureUserContainerViewCell
        cell.featureUsers = featureUser
        cell.selectUserdelegate = self
        return cell
    case 2:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ticketLabelIdent, for: indexPath) as! ticketLabelCell
        return cell
    case 3:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topfeatureCellIndent, for: indexPath) as! topFeatureCell
        //configure if needed
        return cell
    case 4:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: whosgoingIdent, for: indexPath) as! whoGoingCell
        cell.config(withTimer: timeleft)
        return cell
    default:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: allUserCellIdent, for: indexPath) as! allUserCell
        let index = indexPath.item - 4
        let user = allPartyUserArr![index]
        cell.config(withUser: user)
        return cell
    }

}

Hope this helps or else you can google for more better tutorials and solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM