UICollectionView自定义单元格

时间:2017-04-19 03:35:43

标签: swift uicollectionview uicollectionviewcell

我试图在Swift 3.1上构建我的swift程序。 我可以建立它。 但是它无法从Swift 3.1中正常工作。

我收到了以下错误。

'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier SingleItemCell - must register a nib or a class for the identifier

但是,我已经为uicollectionview注册了SingleItemCell。

  override func viewDidLoad() {
      super.viewDidLoad()

      //エリア情報の表示に必要なxibをcollectionviewに登録
      var nib = UINib(nibName: "ItemCell", bundle:nil)
      areaView.register(nib, forCellWithReuseIdentifier:"ItemCell")

      nib     = UINib(nibName: "SingleItemCell", bundle:nil)
      areaView.register(nib, forCellWithReuseIdentifier:"SingleItemCell")

      nib     = UINib(nibName: "SftCollectionReusableView", bundle:nil)
      areaView.register(nib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SftCollectionReusableView")
      areaView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "EmptyView")

      areaView.dataSource = dataSourcedelegate
      areaView.delegate = dataSourcedelegate

      ....
  }


  /**
 セル一つ一つの定義

 - parameter collectionView: <#collectionView description#>
 - parameter indexPath:      <#indexPath description#>

 - returns: <#return value description#>
 */
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    return configureCell(collectionView, cellForRowAtIndexPath: indexPath)
}

/**
 各セルの表示

 - parameter collectionView: <#collectionView description#>
 - parameter indexPath:      <#indexPath description#>

 - returns: <#return value description#>
 */
 func configureCell(_ collectionView: UICollectionView, cellForRowAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.section == 0{

        return areaItemCell(collectionView, indexPath: indexPath)
    }else{
        let cell:SingleItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SingleItemCell", for: indexPath) as! SingleItemCell
        cell.itemLabel.text = "現在地から検索"
        cell.itemLabel.addImage("common_here_icon",font: UIFont.boldSystemFont(ofSize: 20),marginx: -5,intLabelMode: ImageLabelMode.left.hashValue)
        return cell
    }
}

项目单元已加载。 但是,未加载SingleItemCell。 为什么即使我注册也没有加载。

这是什么问题?

1 个答案:

答案 0 :(得分:1)

我可以自己解决。 我附上了我的修复捕获。

enter image description here

我改变了方法的调用顺序。 然后,我的系统正常工作。 我的疑问是

viewModel.areas.asObservable().bindTo(areaView.rx.items(dataSource: dataSourcedelegate))
        .addDisposableTo(disposeBag)
在注册自定义单元格之前调用

上面的方法。

相关问题