2
私はUICollectionを2つのセルしか持たず、 "Show me more"というボタンを含むフッターを作成しました。そして "Show me more"ボタンをクリックすると、UICollectionに2つのセルが追加されます。UICollectionは手動でセルを追加しますSwift
私の問題は、私はUICollectionは、2個の新しい細胞をリロードして追加されません]ボタンをクリックしたときです。私は無限スクロールに関する別の投稿をチェックしましたが、問題を解決できません。ここに私のコードは次のとおりです。
マイボタンと彼の機能
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() }
マイコレクション `
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
}
}
}
`
でそのインスタンスを再利用するすべてのメソッドが正しく呼び出されていることを確信していますか?ボタンをクリックするメソッド 'handleShowMore'が呼び出されますか?本当に増えたユーザーの数ですか?デバッギングが 'numberOfItems'のとき、実際にリロードした後に2ではなく4を返すのですか?プロパティ 'numberOfUser'が 'HomeDatasource'クラスの一部ではないのはなぜですか? – KaraBenNemsi
また、これらの行のオプションは、 'homeDatasource?nincludeNumberOfUser() HomeDatasourceController()。collectionView?.loadData()'ではありませんか? – KaraBenNemsi
はい、handleShowMoreメソッドが呼び出され、numberOfUserがインクリメントされます。しかし、細胞は更新されません。データをリロードする前に、データソースをクリアする必要があるかもしれません。 – Sombrero