繁体   English   中英

在UIcollectionview中使多个单元出队的方法

[英]Way to dequeue multiple cells in UIcollectionview

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

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

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

截图

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

}

您可以像这样在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设置的项目数。

对于每个section cells

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上搜索更多更好的教程和解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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