【发布时间】:2019-06-18 10:59:38
【问题描述】:
我在tableView 单元格中创建了一个collectionView
我注册了所有代表和协议,在大多数情况下一切正常
问题在于collectionView 必须根据服务器响应动态生成单元格
我在集合顶部安装了一个计数器,显示有多少单元格(也动态更新)
如果加载屏幕时集合在视野中,则加载单元格
如果需要完成收集,则为空,尽管计数器指示存在元素。
我不太明白为什么。代码如下
代码形式 VC:
let realPfotoCell = tableView.dequeueReusableCell(withIdentifier:
"RealPhoto", for: indexPath)
as! RealPhotoCarInfoTableViewCell
realPfotoCell.delegate = self
realPfotoCell.model = self.rPhoto
if(self.rPhoto?.data != nil){
realPfotoCell.RPhotoCount.text = String(self.rPhoto.data!.count)
}
cell = realPfotoCell
cell.selectionStyle = UITableViewCell.SelectionStyle.none
}
来自tableViewCell的代码:
var model: RealPhoto!
func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
MyCollection.delegate = self
MyCollection.dataSource = self
MyCollection.reloadData()
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
MyCollection.reloadData()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (self.model?.data?.count != nil){
print("Photos here")
print(self.model?.data!.count as Any)
} else {
print("No photos")
}
return self.model?.data?.count ?? 0
}
【问题讨论】:
-
在收到服务器响应后,您是否重新加载了
collectionview? -
你的意思是collectionView.reloaddata()?
-
是
collectionView.reloaddata() -
我在
awakeFromNib ()函数中执行此操作生成单元后,我没有重新启动,我无法弄清楚代码的哪一部分执行此操作。你能告诉我怎么做吗? -
您需要在收到服务器响应后调用
collectionView.reloaddata(),并将其保存在您的控制器中。
标签: ios swift uitableview uicollectionview