Ios 集合视图单元格未显示
我知道有很多问题可以回答,但我的情况略有不同。大多数答案告诉我,如果您正在使用故事板,请删除这一行代码: collectionView?.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:cellId) 但我不能这样做,因为我同时使用故事板和非故事板 我从编程推送视图控制器(情节提要控制器-选项卡栏控制器)转换到该集合视图控制器(非情节提要视图),如下所示:Ios 集合视图单元格未显示,ios,swift,uicollectionviewcell,collectionview,Ios,Swift,Uicollectionviewcell,Collectionview,我知道有很多问题可以回答,但我的情况略有不同。大多数答案告诉我,如果您正在使用故事板,请删除这一行代码: collectionView?.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:cellId) 但我不能这样做,因为我同时使用故事板和非故事板 我从编程推送视图控制器(情节提要控制器-选项卡栏控制器)转换到该集合视图控制器(非情节提要视图),如下所示: func showChatLogController(
func showChatLogController(user: User) {
let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewLayout())
chatLogController.user = user
chatLogController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(chatLogController, animated: true)
}
我的集合视图代码似乎是编写的,我不知道问题出在哪里。如果需要在此处查看集合视图代码,请参见:
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.whiteColor()
collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
setUpInputComponents()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
cell.backgroundColor = UIColor.blueColor()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: view.frame.height, height: 80)
}
由于某些原因,它不显示单元格。我不知道为什么。任何帮助都将不胜感激
PS:我不知道这是否重要,但要访问集合视图控制器,我来自选项卡栏控制器。在
视图中设置集合视图的数据源和委托为self
yourCollectionView.dataSource = self
yourCollectionView.delegate = self
- 有人呼叫CellForRowatinexPath吗?如果你在里面打印了什么东西,它会被记录下来吗
- 您的收藏视图是否显示在您期望的位置?如果你设置了它的背景色,你看到了吗
- 以sizeForItemAtIndexPath打印返回的大小。这是你所期望的还是一个0在那里偷偷溜进
我最近遇到了这个问题,发现我对集合视图控制器的实例化不正确。我之前将其实例化为:
让CollectionViewController=CollectionViewController(collectionViewLayout:UICollectionViewLayout())
何时应该:
让CollectionViewController=CollectionViewController(collectionViewLayout:UICollectionViewFlowLayout())
它无法识别我的cellForItemAtIndexPath或我的SizeForItemIndexPath。我不知道为什么它会读这些行:numberOfItemsInSection和collectionView?.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier:cellId)何时设置集合视图的数据源?我想知道collection视图是否在单元格注册以供重用之前尝试重新加载数据。如果在registerClass之后调用reloadData(),会发生什么情况?其他人对我的非常奇怪的情况有什么想法吗?其他人对我的非常奇怪的情况有什么建议吗?其他人对我的非常奇怪的情况有什么建议吗?Hi Rohan,你解决了这个问题吗?我们似乎有完全相同的事情发生。我还注意到,如果我开始滚动单元格应该显示的位置,它们会突然出现。