2
votes

I have made a UICollection who contain only 2 cells and with a footer who contain a button "Show me more". And when we click on the button "Show me more" it should append 2 more cells in the UICollection.

screen

My problem is when I click on the button the UICollection doesn't reload and append the 2 new cells. I checked different post about infinite scrolling, but I can't resolve my problem. Here is my code:

My button and his function

    lazy var showMoreButton: UIButton = {
            let button = UIButton(type: .system)
            button.setTitle("Show me more", for: .normal)
            button.addTarget(self, action: #selector(handleShowMore), for: .touchUpInside)
            return button
        }()

    func handleShowMore(){

            homeDatasource?.incrementNumberOfUser()
            HomeDatasourceController().collectionView?.reloadData()  
        }

My collection `

var numberOfUser: Int = 2

class HomeDatasource: Datasource, JSONDecodable { 

    override func footerClasses() -> [DatasourceCell.Type]? {
        return [UserFooter.self]
    }


    override func cellClasses() -> [DatasourceCell.Type] {
        return [UserCell.self, TweetCell.self] //UserCell -> section 0 and TweetCell -> section 1
    }

    override func item(_ indexPath: IndexPath) -> Any? {
        if indexPath.section == 1 {
            return tweets[indexPath.item]
        }
        return users[indexPath.item]
    }  

    override func numberOfItems(_ section: Int) -> Int {
        if section == 1 {
            return tweets.count
        }
        return numberOfUser //users.count
    }

    override func numberOfSections() -> Int {
        return 2
    }

    func incrementNumberOfUser() {

        if numberOfUser <= users.count {
            numberOfUser = numberOfUser + 2
        }
    }


}

`

1
are you sure that all the methods are called correctly? is the button click method 'handleShowMore' called? is the number of users really incremented? when debugging is 'numberOfItems' really returning 4 instead of 2 after reloading? why is the property 'numberOfUser' not part of your 'HomeDatasource' class? - chriswillow
are you also sure that the optionals in theses lines are not nil 'homeDatasource?.incrementNumberOfUser() HomeDatasourceController().collectionView?.reloadData()'? - chriswillow
Yes, the method handleShowMore is called and the numberOfUser is incremented. But the cells are not updated. Maybe I need to clear the datasource, before reload the data. - Sombrero
so the first time when the collectionview is being built the method 'numberOfItems' returns 2 and the second time after you pressed the button the method 'numberOfItems' is called again and returns 4. Is this correct? - chriswillow
and could you please post the implementation of 'UICollectionViewDataSource' delegate methods? - chriswillow

1 Answers

1
votes

this line

HomeDatasourceController().collectionView?.reloadData()  

seems to be the cause. Because you are creating a new 'HomeDatasourceController' although at the time of reloading it should already exist. So you should reuse the already existing instance of 'HomeDatasourceController'.

Somewhere you created an instance e.g.

let datasourceController = HomeDatasourceController()

now reuse that instance in

func handleShowMore() {
    homeDatasource?.incrementNumberOfUser()
    datasourceController.collectionView?.reloadData()  
}