我想通过说我已经习惯了UITableViews如何工作并且对UICollectionView不太熟悉来作为序言。
我基本上做的是
featuredCollectionView = UICollectionView(frame: CGRect(x: 0, y: featuredLabel.frame.origin.y + featuredLabel.frame.size.height, width: view.frame.size.width, height: 216), collectionViewLayout: layout)
featuredCollectionView.delegate = self
featuredCollectionView.dataSource = self
featuredCollectionView.register(ItemCollectionViewCell.self, forCellWithReuseIdentifier:
"cell")
featuredCollectionView.backgroundColor = UIColor.fysGray
featuredCollectionView.showsHorizontalScrollIndicator = false
// add view to screen
func fetchPosts()
{
// note some libs have been changed for x purposes
NetworkClass.loadPosts()
{ (data, error) in
// parse into valid object
// add to datasource array
self.featuredItems.append(item)
// reload data
DispatchQueue.main.async
{
self.featuredCollectionView.reloadData()
}
}
collectionView.reloadData()
如上所示调用重新加载数据后,屏幕上不显示任何内容,就像它是空白一样。我尝试从主队列重新加载并插入/执行批量更新等。我真的很困惑如何使其工作。从网络加载之前,我是否必须知道项目数量?我怎样才能使这个工作?
这里是cellForItemAt的代码
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
var item: Item
if collectionView == featuredCollectionView
{
item = featuredItems[indexPath.row]
}
else
{
item = recentlyViewedItems[indexPath.row]
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ItemCollectionViewCell
cell.item = item
return cell
}
答案 0 :(得分:0)
结束了单元格方法中if条件的问题。由于某种原因,它不喜欢它,所以我使用了两个带有容器视图的独立视图控制器,它就像一个带有reloadData()
的魅力