ios - 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?
解决方案
我建议您在 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
}
}
推荐阅读
- azure - 如何让我的 AKS 群集向我的 ACR 进行身份验证?
- python-asyncio - 来自 aiohttp.ClientSession().post() 的带有多个图像文件的格式错误的请求
- java - Eclipse IDE 项目资源管理器窗口中的奇怪字符
- c++ - 在 Linux 终端中使用 `std::wstring` 和 `std::wcout` 打印拉丁字符
- twitter-bootstrap - Bootstrap 4 布局设计
- android - Android Studio 无法解决:.espresso:espresso-core:3.2.0
- excel - 将 300 张数据表添加到 Excel 的快速方法是什么?
- kubernetes - Kubernetes 变化原因
- sql - SQL如何查找随时间超过一个值的连续行数
- python - 使用tornado websockets时如何处理协程