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.
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
}
}
}
`