xcode UICollectionView 中的多个部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44691696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Section in UICollectionView
提问by christ2702
I was building an iOS app for my hospital using collection view. However, I need to use multiple sections for the specialist clinic depends on the purpose. I already completed the code if it's just for 1 section. when I try to make it 2 sections, it always returns a nil value.
我正在使用集合视图为我的医院构建一个 iOS 应用程序。但是,我需要为专科诊所使用多个部分,这取决于目的。如果只是 1 个部分,我已经完成了代码。当我尝试将它分成 2 个部分时,它总是返回一个 nil 值。
please check my code below
请在下面检查我的代码
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if indexPath.section == 0 {
var buttonSpecialist = cell.viewWithTag(1) as! UIButton
buttonSpecialist.setTitle(list[indexPath.row].SpecialtyName, for: .normal)
let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list[indexPath.row].ImageUrl)!))
var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50))
buttonSpecialist.setImage(newimg, for: .normal)
buttonSpecialist.contentMode = .center
buttonSpecialist.imageView?.contentMode = .scaleAspectFit
//let sign: Int = imageOnTop ? 1 : -1
let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size
buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0)
let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size
buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!)
cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.black.cgColor
}
else if indexPath.section == 1 {
var buttonSpecialist = cell.viewWithTag(1) as! UIButton
buttonSpecialist.setTitle(list2[indexPath.row].SpecialtyName, for: .normal)
let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list2[indexPath.row].ImageUrl)!))
var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50))
buttonSpecialist.setImage(newimg, for: .normal)
buttonSpecialist.contentMode = .center
buttonSpecialist.imageView?.contentMode = .scaleAspectFit
//let sign: Int = imageOnTop ? 1 : -1
let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size
buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0)
let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size
buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!)
cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.black.cgColor
}
The main problem here is, I didn't know how to set the specific cell for the specific section. I already using 'IF' inside the function 'cellforindexpath', but it didn't worked.
这里的主要问题是,我不知道如何为特定部分设置特定单元格。我已经在函数“cellforindexpath”中使用了“IF”,但它没有用。
this is my return number of items in section
这是我在部分中返回的项目数
func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
if section == 0 {
return list.count
}
else if section == 1{
return list2.count
}
return 0
}
please help me
请帮我
回答by Itachi Uchiha
Make sure assigning DataSource & Delegate to CollectionView
确保将 DataSource 和 Delegate 分配给 CollectionView
1.Give number of sections you wanna show using below method
1.使用以下方法给出要显示的部分数量
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
2.Set item count of each sections for CollectionView using below method.
2.使用以下方法为 CollectionView 设置每个部分的项目数。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (section == 0) ? list.count : list2.count
}
3.Assigning cell for each item to CollectionView.
3.将每个项目的单元格分配给CollectionView。
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//If you are using multiple cells based on section make condition
if indexPath.section == 0 {
//make sure the identifier of your cell for first section
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath)
// do your stuffs
return cell
}else{
//make sure the identifier of your cell for second section
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath)
// do your stuffs
return cell
}
}
回答by Krishna Kumar
If you have different cells layout requirement in that case you should use different custom cells with different reuse identifier. As I can see in your code you have same layout of cell with different datasource.
如果在这种情况下您有不同的单元格布局要求,您应该使用具有不同重用标识符的不同自定义单元格。正如我在您的代码中看到的,您具有相同的单元格布局和不同的数据源。
So I would say try to subclass the collectionview cell and put a IBOutlet property for button through xib or storyboard in the subclass.
所以我会说尝试对 collectionview 单元格进行子类化,并通过子类中的 xib 或故事板为按钮放置一个 IBOutlet 属性。
You can refer this question: how to create custom UICollectionViewCell
您可以参考这个问题: 如何创建自定义 UICollectionViewCell