UICollectionView中的多个部分

时间:2017-06-22 06:26:02

标签: ios swift xcode uicollectionview

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

请查看下面的代码

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
    }

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

这是我在

部分中返回的项目数
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

}

请帮帮我

2 个答案:

答案 0 :(得分:2)

  

确保分配DataSource&委托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
      }

}

答案 1 :(得分:0)

如果在这种情况下有不同的单元格布局要求,则应使用具有不同重用标识符的不同自定义单元格。正如我在您的代码中看到的那样,您具有相同的具有不同数据源的单元格布局。

所以我想说尝试子类化collectionview单元格,并在子类中通过xib或storyboard放置一个IBOutlet属性。

你可以参考这个问题: how to create custom UICollectionViewCell