我尝试为我的集合视图创建自定义单元格。 我有一个名为AccountCell
的xib:
在包含集合视图的视图中:
profileViewController = p;
collectionView.dataSource = profileViewController
collectionView.registerClass(SocialAccountCell.self, forCellWithReuseIdentifier: "accountCell")
然后在数据源中:
func collectionView(_collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
println("getting cell")
let cell = _collectionView.dequeueReusableCellWithReuseIdentifier("accountCell", forIndexPath: indexPath) as! SocialAccountCell
cell.test()
return cell
}
func collectionView(_collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return user!.accounts.count
}
SocialAccountCell.swift
:
class SocialAccountCell: UICollectionViewCell
{
func test()
{
println("Hello From collection cell")
}
}
在集合视图中没有出现蓝色的单元格,但是"getting cell"
和"Hello From collection cell"
都被打印出来。我尝试在单元格中添加图片以检查是否只是蓝色无法显示,但仍然没有任何单元格的迹象。
我错过了哪些步骤?
collectionView.registerClass(SocialAccountCell.self, forCellWithReuseIdentifier: "accountCell")
应用程序会崩溃。 - Deekorfunc collectionView(_collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return user!.accounts.count }
中硬编码单元格数量吗? - L.C. Tan