Xib文件中的CollectionView单元格未显示。

4

我尝试为我的集合视图创建自定义单元格。 我有一个名为AccountCell的xib:

enter image description here

在包含集合视图的视图中:

    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"都被打印出来。我尝试在单元格中添加图片以检查是否只是蓝色无法显示,但仍然没有任何单元格的迹象。

我错过了哪些步骤?


你是否在自定义单元格的xib上设置了重用标识符? - FormigaNinja
@FormigaNinja 是的,但似乎没有任何作用,因为如果没有 collectionView.registerClass(SocialAccountCell.self, forCellWithReuseIdentifier: "accountCell") 应用程序会崩溃。 - Deekor
只有蓝色部分没有显示吗?您能看到单元格的任何指示吗? - Icaro
据我所知,没有单元格。@IcaroNZ - Deekor
你尝试过在 func collectionView(_collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return user!.accounts.count } 中硬编码单元格数量吗? - L.C. Tan
尝试使用registerNib而不是registerClass。 - Icaro
1个回答

9

因为你使用xib定义单元格,所以我认为你会使用collectionView.registerNib(UINib(nibName: "someNib", bundle: nil), forCellWithReuseIdentifier: "accountCell"),否则你需要在代码中设置UI元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接