Ios UICollection手动加载更多传感器Swift
我制作了一个UICollection,其中只包含2个单元格,页脚中包含一个按钮“显示更多”。当我们点击“显示更多”按钮时,它会在UICollection中追加2个单元格 我的问题是,当我单击按钮时,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() } 惰性变量showMoreButton:UIButton={ let button=ui按钮(类型:。系统) setTitle(“显示更多”,用于:。正常) button.addTarget(self,action:#选择器(handleShowMore),for:.touchUpInside) 返回按钮 }() func handleShowMore(){ homeDatasource?.incrementNumberOfUser() HomeDatasourceController().collectionView?.reloadData() } 我的收藏 `Ios UICollection手动加载更多传感器Swift,ios,swift,swift3,uicollectionview,Ios,Swift,Swift3,Uicollectionview,我制作了一个UICollection,其中只包含2个单元格,页脚中包含一个按钮“显示更多”。当我们点击“显示更多”按钮时,它会在UICollection中追加2个单元格 我的问题是,当我单击按钮时,UICollection不会重新加载并附加2个新单元格。我查看了关于无限滚动的不同帖子,但我无法解决我的问题。这是我的密码: 我的按钮和他的功能 lazy var showMoreButton: UIButton = { let button = UIButton(type:
var numberOfUser:Int=2
类HomeDatasource:Datasource,JSONDecodable{
重写func footerClasses()->[DatasourceCell.Type]{
返回[UserFooter.self]
}
重写func cellClasses()->[DatasourceCell.Type]{
返回[UserCell.self,TweetCell.self]//UserCell->section 0和TweetCell->section 1
}
重写func项(\uIndeXPath:indexPath)->Any{
如果indexath.section==1{
返回推文[indexPath.item]
}
返回用户[indexPath.item]
}
重写func numberOfItems(u部分:Int)->Int{
如果节==1{
返回tweets.count
}
返回numberOfUser//users.count
}
重写func numberOfSections()->Int{
返回2
}
func incrementNumberOfUser(){
如果numberOfUser此行
HomeDatasourceController().collectionView?.reloadData()
似乎是原因。因为您正在创建一个新的“HomeDatasourceController”,尽管在重新加载时它应该已经存在。因此,您应该重用“HomeDatasourceController”的现有实例
您在某处创建了一个实例,例如
let datasourceController = HomeDatasourceController()
现在在中重用该实例
func handleShowMore() {
homeDatasource?.incrementNumberOfUser()
datasourceController.collectionView?.reloadData()
}
你确定所有方法都被正确调用了吗?按钮点击方法“handleShowMore”被调用了吗?用户的数量真的增加了吗?当调试时,“numberOfItems”在重新加载后返回4而不是2?为什么属性“numberOfUser”不是“HomeDatasource”类的一部分?你也确定选项在这些行不是零'homeDatasource?.incrementNumberOfUser()HomeDatasourceController().collectionView?.reloadData()“?是的,方法handleShowMore被调用,numberOfUser被递增。但是单元格没有更新。可能我需要在重新加载数据之前清除数据源。因此,第一次构建collectionview时,方法“numberOfItems”返回2,第二次按下按钮后,方法“numberOfItems”是cal再次led并返回4。这是否正确?您能否发布“UICollectionViewDataSource”委托方法的实现?
func handleShowMore() {
homeDatasource?.incrementNumberOfUser()
datasourceController.collectionView?.reloadData()
}