数组值中的collectionView中的节标题

时间:2019-04-22 09:15:11

标签: swift swift5

我有一个colectionView,它显示57个项目,但我希望将这些项目分成小组,每个组都有一个Header部分。这些项目以数组的形式设置,每个ID的ID为1到57。如何将这些项目分类为带有标题的节,例如从项目1到项目7,依此类推?

我的CollectionView代码是:

extension OLLViewController: UICollectionViewDataSource, UICollectionViewDelegate {


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {  //set the amount of items in the CollectionView to the amount of items in the OLLData dictionary
    if (section == 2) {
        return 2
    }
    return 0
    //return OLLData.OLLCasesList.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {  //set each cell to a different mamber of the dict.
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OLLCell", for: indexPath) as! OLLCell
    cell.imageView.backgroundColor = OLLData.OLLCasesList[indexPath.item]._isSelected ? UIColor.init(red: 101.0/255.0, green: 177.0/255.0, blue: 94.0/255.0, alpha: 0.9) : UIColor.clear //change colour if selected

    let image = OLLData.OLLCasesList[indexPath.item]._imageName

    cell.label.text = image
    cell.imageView.image = UIImage(named: image)


    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)  { //detect if case selected and reload CollectionView
    let caseName = OLLData.OLLCasesList[indexPath.item]._imageName
    print(caseName, OLLData.OLLCasesList[indexPath.item]._isSelected)
    OLLData.OLLCasesList[indexPath.item]._isSelected = !OLLData.OLLCasesList[indexPath.item]._isSelected
    collectionView.reloadItems(at:[indexPath])

    if OLLData.OLLCasesList[indexPath.item]._isSelected == true { //if the item is selected, add to selectedCases array
        selectedCases.append(OLLData.OLLCasesList[indexPath.item]._id)
        selectedCaseNames.append(OLLData.OLLCasesList[indexPath.item]._imageName)
        print(selectedCases, selectedCaseNames) //debugging
        numberOfSelectedCases.text = String(selectedCases.count)
    }
    else if OLLData.OLLCasesList[indexPath.item]._isSelected == false { //remove from selectedCases array
        selectedCases.removeAll(where: { $0 == OLLData.OLLCasesList[indexPath.item]._id })
        selectedCaseNames.removeAll(where: { $0 == OLLData.OLLCasesList[indexPath.item]._imageName })
        print(selectedCases, selectedCaseNames) //debugging
        numberOfSelectedCases.text = String(selectedCases.count)
    }
}

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

 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    var reusableView = UICollectionReusableView()
    if (kind == UICollectionView.elementKindSectionHeader) {
        let section = indexPath.section

        switch(section) {
        case 1:
            let firstHeader : HeaderReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OLLHeaderView", for: indexPath) as! HeaderReusableView
            reusableView = firstHeader

        case 2:
            let secondHeader : HeaderReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: "", withReuseIdentifier: "OLLHeaderView", for: indexPath) as! HeaderReusableView
            reusableView = secondHeader

        default:
            return reusableView
        }


    }
    return reusableView


}

}

有什么方法可以做到这一点?

0 个答案:

没有答案