ios - 在 UIcollectionview 中出列多个单元格的方法

标签 ios swift uicollectionview

根据屏幕截图,我将有一个包含少量单元格(带颜色)的大 Collection View 。除绿色单元格外,所有单元格在 View 中仅显示一次。绿色单元格将显示一组用户。

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

我需要显示最后一个单元格的方法是实现上面的代码,但我认为它不正确,因为如果我想在显示所有单元格后添加其他单元格,有没有更好的方法使单元格出列合适吗?

screenshot

最佳答案

我建议您在 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
  }

关于ios - 在 UIcollectionview 中出列多个单元格的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50442507/

相关文章:

ios - 如何从本地路径加载图像 ios swift(按路径)

ios - UICollectionView 单元格水平对齐而不是垂直对齐以模仿跳板?

swift - 单击 UICollectionViewCell 打开另一个屏幕

iphone - 如何获取以英寸为单位的UIImage大小或UIImage分辨率?

ios - 使用 Swift Realm Results<> 或 List<> 实例时,如何避免延迟或调整 coordinator.drop 的时间?

ios - 为什么此线程不执行最后一行代码

swift - Swift 中 “+=” 运算符的内存安全

ios - UICollectionViewCell 无法将值分配给 CustomCollectionViewCell

ios - 使用密码从URL提取数据

用于日期自然语言处理的 iOS 库?