Ios UICollectionView无法正确重新加载
我正在处理swift 3中的一个项目,它在UITableView中包含一个UICollectionView。因此,尽管我设法将所有属性分配给我从服务器获得的UICollectionViewCell类,但一旦我运行项目,除非我滚动UITableView,否则数据将不会显示在单元格上。三个班级的结构如下。非常感谢您的帮助Ios UICollectionView无法正确重新加载,ios,uitableview,swift3,uicollectionview,uicollectionviewcell,Ios,Uitableview,Swift3,Uicollectionview,Uicollectionviewcell,我正在处理swift 3中的一个项目,它在UITableView中包含一个UICollectionView。因此,尽管我设法将所有属性分配给我从服务器获得的UICollectionViewCell类,但一旦我运行项目,除非我滚动UITableView,否则数据将不会显示在单元格上。三个班级的结构如下。非常感谢您的帮助 extension ViewController : UITableViewDelegate, UITableViewDataSource { func numberOfSect
extension ViewController : UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.allCategoriesTableView{
return 4
} else {
return 1
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.allCategoriesTableView {
return 1
}else if tableView == self.categoryTableView {
return categoryArray.count
}else{
return menuBarArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
if tableView == self.allCategoriesTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "newCell", for: indexPath) as! CollectionItemsTableViewCell
if indexPath.section == 0 {
cell.fillCollectionView(with: category1)
}else if indexPath.section == 1 {
cell.fillCollectionView(with: category2)
}else if indexPath.section == 2 {
cell.fillCollectionView(with: category3)
} else {
cell.fillCollectionView(with: category3)
}
return cell
}else if tableView == self.categoryTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! CategoryTableViewCell
cell.categoryNameLabel.text = categoryArray [indexPath.row]
cell.layer.cornerRadius = 8
cell.layer.masksToBounds = true
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuBarCell", for: indexPath) as! MenuBarTableViewCell
cell.menuBarContentLabel.text = menuBarArray [indexPath.row]
return cell
}
}
UITableViewCell类
class CollectionItemsTableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
var category1Json = [JSON?]()
func fillCollectionView(with array: [JSON?]) {
self.category1Json = array
self.collectionView.reloadData()
}
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.dataSource = self
loadCategoryDetails()
print("my array col is :",category1Json)
self.collectionView.reloadData()
}
extension CollectionItemsTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("collection count is :", category1Json.count)
return category1Json.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! AbundanceAndLifePurposeCollectionViewCell
if category1Json.count>0{
cell.titleLabel.text = category1Json[indexPath.row]?["title"].stringValue
cell.healerNameLabel.text = category1Json[indexPath.row]?["healerName"].stringValue
cell.teaserDescriptionLabel.text = category1Json[indexPath.row]?["teaserDescription"].stringValue
cell.priceLabel.text = category1Json[indexPath.row]?["price"].stringValue
cell.musicImageView.image = imageArray [indexPath.row]
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let clickedIndex = songArray[indexPath.row]
print(clickedIndex)
}
您可以尝试以下代码 调度\u异步调度\u获取\u主队列{ self.collectionView.reloadData} 而不是 self.collectionView.reloadData
为什么要调用api,因为您没有使用它的数据?很抱歉,我编辑了codeTry一次,从cellForItemAt方法中删除了计数检查if条件,并直接设置了标签,因为单元格将被重用。仍然相同,没有区别,先生,我应该在CellforRow中执行此操作吗?很抱歉,调度\u asyncdispatch\u get\u main\u队列{self.collectionView.reloadData}而不是self.collectionView.reloadData