【问题标题】:Way to dequeue multiple cells in UIcollectionview在 UIcollectionview 中使多个单元格出列的方法
【发布时间】:2018-05-21 05:31:29
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么不对不同的类型使用不同的部分?
  • 我是 swift 新手,你能告诉我如何在不同的部分显示它们吗?谢谢兄弟
  • 查看本教程:raywenderlich.com/136161/…
  • @Amit 我不认为本教程提供的信息与我想要的相似,因为它们主要关注标题和选择。
  • @MuhammadNayab 有关如何正确实施部分的任何指南?

标签: ios swift uicollectionview


【解决方案1】:

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

这是你可以设置节数的方法

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

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

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
  }

}

【讨论】:

  • 兄弟你能解释一下“return (section == 0) 吗?4:users.count”,我不明白
  • 我只是使用了三元运算符。请参阅编辑后的答案。在第 0 节的情况下,我们有 4 个单元格(一次性显示)。在第 1 节的情况下,我们将根据要显示的用户数返回单元格。 (我假设“用户”是我们存在用户的数组)
  • 谢谢...我遇到的另一个问题是 cellforitem 向我返回一个错误,“预期返回 'UICollectionViewCell' 的函数中缺少返回”
  • 检查您的 if/else 语句 在所有情况下都必须返回一个单元格。否则你会得到这个错误
  • 我使用的正是你提供的那个,我认为 if else 是正确的,虽然很奇怪
【解决方案2】:

您可以像这样设置CollectionView 中的部分数量:

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

设置每个section中的项目数:

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

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

您可以根据section设置的项目数。

对于每个cells 中的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
    }

}

希望这对您有所帮助,否则您可以在 Google 上搜索更多更好的教程和解决方案。

【讨论】:

  • 兄弟,所以您认为 5 个部分比仅 2 个部分更好,对吧?
  • 我将根据您在问题中添加的代码为您提供使用不同部分的实现。您可以根据自己的需要进行更改。
  • @muhammadnayab 回答也是一个很好的实现方式。
猜你喜欢
  • 2021-09-25
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多