首页 > 解决方案 > 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?

screenshot

标签: iosswiftuicollectionview

解决方案


我建议您在 UICollectionView 中使用 2 个部分。保留第 0 节中的所有一次性可见单元格和第 1 节中代表用户数组的单元格

这是您可以设置部分数量的方法

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

要设置每个部分的标题,您可以实现以下功能并为标题设置任何大小。CGSizeMake(0, 0) 将隐藏标题

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

然后每个部分中的项目数

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

显示单元格

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
  }

}


推荐阅读