user
user

Reputation: 83

swift: collectionView section

I have 7 section in collectionView. I want to show image in all section except first and last.

I use this code to do it:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    if sectionNumber == 0 {
        //hide image
    } else {
        //show image
    }

    if sectionNumber == 6 {
        //hide image
    } else {
        //show image
    }

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        sectionNumber = indexPath.section
        print("indexPath.section\(indexPath.section)")
}

But code doesn't work. When I scroll to last section my image hides in 6 section(5 in code because first section == 0). And if I scroll to first section my image not hides.

Also I have second variant of code. In this variant all works fine except when I scroll to first section my image hides in second.

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let currentlyVisibleSection = collectionView.indexPathsForVisibleItems.map { $0.section }.min()!

        if currentlyVisibleSection < 5 {
            let scrollToSection = currentlyVisibleSection + 1
            //show image
        }

        if currentlyVisibleSection == 5 {
            let scrollToSection = currentlyVisibleSection + 1
            //hide image
        }

        if currentlyVisibleSection != 1 {
            let scrollToSection = currentlyVisibleSection - 1
            //show image
        }

        if currentlyVisibleSection == 1 {
            let scrollToSection = currentlyVisibleSection - 1
            //hide image
        }
}

Update

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.section == 0 {
            print("firstSection")
        }

        if (indexPath.section + 1) == dataModel.count {
            print("lastSection")
        }
}

Upvotes: 0

Views: 680

Answers (2)

Hendra Kusumah
Hendra Kusumah

Reputation: 186

Have you try reloadData() after scrollViewDidEndDecelarating?

I try to propose another approach:

 override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

            guard let cell = cell as? CollectionViewCell else {return}

            switch indexPath.section {
            case 0,6: //in assumption number of sections = 7
                cell.myImage.isHidden = true
            default:
                cell.myImage.isHidden = false
            }
        }

Upvotes: 1

Martin Prusa
Martin Prusa

Reputation: 211

I am not sure what is hiding behind //hide image and //show image, but why not to move the logic into the cell itself? Every time when the cell is displayed it reuse the old one and cellForItemAt gets called. So either there or in the cell you can determine what row you are displaying.

So you can create a simple function where you'd determine whether this row is first or last.

func displayImage(on indexPath: IndexPath) -> Bool {
    if indexPath.section == 0 {
        return false
    }

    if (indexPath.section + 1) == dataModel.count {
        return false
    }

    return true
}

dataModel would have to be a two levels array

let dataModel = [["item1", "item2", "item3"], ["item4", "item5", "item6"]]

where each sub array would represent one section.

Upvotes: 2

Related Questions