-1

UICollectionViewのUICollectionViewLayoutを設定したいが、レイヤーが設定されているときにcellForItemAt関数が呼び出されない。なぜか分からない。私が持ってしようとしたUICollectionViewLayerが設定されているときにcellForItemAtが呼び出されない

class DetailGroupCollectionviewLayout: UICollectionViewLayout { 
    let CELL_HEIGHT = 30.0 
    let CELL_WIDTH = 100.0 

    var cellAttrsDictionary = Dictionary<IndexPath,UICollectionViewLayoutAttributes>() 
    var contentSize = CGSize.zero 

    override var collectionViewContentSize: CGSize { 
     return self.contentSize 
    } 

    override func prepare() { 
     if let lCollectionView = self.collectionView { 
      if lCollectionView.numberOfSections > 0 { 
       for section in 0...lCollectionView.numberOfSections - 1 { 
        if lCollectionView.numberOfItems(inSection: section) > 0 { 
         for item in 0...lCollectionView.numberOfItems(inSection: section) - 1 { 
          let cellIndex = IndexPath(item: item, section: section) 
          let xPos = Double(item) * CELL_WIDTH 
          let yPos = Double(section) * CELL_HEIGHT 

          let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex) 
          cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT) 
          cellAttributes.zIndex = 1 

          cellAttrsDictionary[cellIndex] = cellAttributes 
         } 
        } 
       } 
      } 
     } 
    } 
} 

required init?(coder aDecoder: NSCoder) 
{ 
    super.init(coder: aDecoder) 
    xibSetup() 

    self.collectionView.register(UINib(nibName: "StepCell", bundle: nil), forCellWithReuseIdentifier: stepCellIdentifier) 
    self.collectionView.delegate = self 
    self.collectionView.dataSource = self 
} 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 3 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 100 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if let lCell = collectionView.dequeueReusableCell(withReuseIdentifier: stepCellIdentifier, for: indexPath) as? StepCell { 
     lCell.text.text = indexPath.row.description 
     return lCell 
    } 
    return UICollectionViewCell() 
} 

私は別のXIBでカスタムUICollectionViewCellを持っており、これは私のUICollectionViewLayoutクラスです: 私UICollectionViewはUICollectionViewLayoutをテストするため、このコードで、XIBでありますストーリーボードのUICollectionViewに関するすべては、私はxibの "バグ"と思っていたが、これは何も解決しない。

私は正しくまたは特殊性を作っていないことがありますか?道に迷いました。

私はスウィフト3.1とXcodeの8.3上だと私のアプリは、iOSの10

答えて

0

に合格していない、私のカスタムUICollectionViewLayoutは何のcontentSizeを持っていない、私は私の準備()関数でこれを追加しました:

let contentWidth = Double(collectionView!.numberOfSections) * CELL_WIDTH 
let contentHeight = Double(collectionView!.numberOfItems(inSection: 0)) * CELL_HEIGHT 
self.contentSize = CGSize(width: contentWidth, height: contentHeight) 

This is the result

0

上では、あなたのReuseIdentifierが正しいことを確認していますか?もしあなたがここにいればここに入るとしようとしましたか? (print文またはブレークポイント付き)?あなたのプロジェクト、またはその一部を共有することができれば

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if let lCell = collectionView.dequeueReusableCell(withReuseIdentifier: stepCellIdentifier, for: indexPath) as? StepCell { 
     lCell.text.text = indexPath.row.description 
     return lCell 
    } 
    return UICollectionViewCell() 
} 

本当にわからない、それは何ができるか、私は喜んであなたとそれを把握しようと思います。また、UICollectionViewDataSourceUICollectionViewDelegateに準拠していることを確認してください。

0

デリゲートの属性はviewControllerですか?それは1つの可能性があります。 (そうでない場合は、「numberOsSections」も呼び出されません)。

0

ReuseIdentifierは正しいです。問題は、UICollectionViewLayoutを追加したときにcellForItemAtが呼び出されない場合です。完璧に実行UICollectionView。私はあなたを伝えるのを忘れて

Without UICollectionViewLayout

With UICollectionViewLayout

何かを、それは問題でUICollectionViewLayoutが設定されたりされていないいないとき、私xibControllerは「numberOfItemsInSection」と「numberOfSections」BUのみに入りますUICollectionViewLayoutが設定されている場合、conトローラが、私は解決策を見つけるcellForItemAt

関連する問題