2017-05-11 3 views
3

기본 ViewController에서 UICollectionView (collectionView)를 구현하고 있습니다. 내 콜렉션 뷰는 numbersofItemInSection 코드 에서처럼 5 개의 셀을 검색해야합니다. CollectionView가 표시되고 numbersofItemInSection 함수가 호출되었지만 cellForItemAt 함수가 호출되지 않아 콜렉션 뷰가 비어 있습니다.UICollectionView cellForItem이 호출되지 않았습니다.

import UIKit 


private let reuseIdentifier = "Cell" 



class TestViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{ 

    lazy var collectionView : UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.translatesAutoresizingMaskIntoConstraints = false 
     cv.delegate = self 
     cv.dataSource = self 
     return cv 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Register cell classes 
     self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 

     collectionView.backgroundColor = .white 
     navigationItem.title = "test" 

     setupViews() 
    } 

    func setupViews(){ 
     view.addSubview(collectionView) 

     collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
     collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true 
     collectionView.heightAnchor.constraint(equalToConstant: 60).isActive = true 
     collectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 
     // Configure the cell 

     cell.backgroundColor = .red 

     return cell 
    } 


} 
+0

예상대로 작동합니다. http://imgur.com/a/IYslT –

+0

xib를 사용하여 셀을 사용 했습니까? – KKRocks

+0

@SaurabhYadav 내 Xcode 설정이 가능합니까? 어제 Xcode 7을 설치 했으므로 이제 2 개의 Xcode 앱이 있습니다. – zorobabel

답변

0

collectionView의 높이 또는 collectionView의 프레임 문제이므로 collectionView에 적절한 높이를 설정해야합니다.

또는

 override func viewDidLoad() { 
       super.viewDidLoad() 
       navigationItem.title = "test" 

       setupViews() 
       collectionView.backgroundColor = .white 
       self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
// remove this line if you not use xib 

      } 
+0

당신이 바로 @KKRocks 있었다 난 그냥 내 컬렉션 뷰의 높이와 세포가 나타납니다 증가. 감사 모든 :) – zorobabel

+0

에 오신 것을 환영합니다 @ 조로 바벨 – KKRocks

0

내가 체크 한 코드를 시도하고 유 신속한 사용하는 fine.As을하고있다 (3) 그래서 당신은 엑스 코드 8 +가 함께 .RUN 사용해야합니다 XCODE 8 구문은 swift 3 용이므로 결과를 얻을 수 있습니다.

관련 문제