gpt4 book ai didi

ios - UICollectionView 中的多个部分

转载 作者:搜寻专家 更新时间:2023-11-01 06:33:20 25 4
gpt4 key购买 nike

我正在使用 Collection View 为我的医院构建一个 iOS 应用程序。但是,我需要为专科诊所使用多个部分取决于目的。如果只是 1 个部分,我已经完成了代码。当我尝试将其分为 2 个部分时,它总是返回一个 nil 值。

请检查下面我的代码

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
}

这里的主要问题是,我不知道如何为特定部分设置特定单元格。我已经在函数“cellforindexpath”中使用了“IF”,但它没有用。

这是我返回的部分中的项目数

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

}

请帮帮我

最佳答案

Make sure assigning DataSource & Delegate to CollectionView

1.使用以下方法给出您想显示的部分数量

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

2.使用以下方法为CollectionView设置每个部分的项目数。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (section == 0) ? list.count : list2.count
}

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
}

}

关于ios - UICollectionView 中的多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com