2017-10-20 19 views
0

collectionView가 표시되는 inputView가 있습니다. 그 inputView에 자동 레이아웃을 사용했습니다. 앱을 실행할 때 numberOfItemsInSection은 호출되지만 cellForItemAt는 호출되지 않습니다. 따라서 셀이 표시되지 않습니다. 제약 조건을 사용하여 잘못 했나요? 또한 프로젝트 https://drive.google.com/file/d/0B5UHWsK1E6dSRDA5bmtSY2ZZbGs/viewcellForItemAt가 collectionView에서 호출하지 않습니다.

에게 ViewController.swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    textField.inputView = InputPhotoView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 265)) 
    textField.becomeFirstResponder() 
} 

InputPhotoView.swift

class InputPhotoView: UIView { 
let CellIdentifier = "Cell" 
override init(frame: CGRect) { 
    super.init(frame: frame) 
    collectionView.dataSource = self 
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: CellIdentifier) 
    setupViews() 
} 
required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    fatalError("Not yet implented") 
} 
func setupViews() { 
    self.backgroundColor = UIColor.brown 
    self.addSubview(collectionView) 
    setupConstraints() 
} 
func setupConstraints() { 
    NSLayoutConstraint.activate([ 
     collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50), 
     collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0), 
     collectionView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0), 
     collectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0) 
     ]) 
} 
public lazy var collectionView: UICollectionView = { 
    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 
    layout.itemSize = CGSize(width: 100, height: 100) 
    layout.minimumInteritemSpacing = 0 
    layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) 
    let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout) 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) 
    return collectionView 
}() 

} 데이터 소스

extension InputPhotoView : UICollectionViewDataSource { 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    print("number of items") 
    return 10 
} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! CustomCell 
    return cell 
} 

}

,536,913,632을 만들었습니다 10

답변

0

코드에 제약 조건 충돌이있어 collectionView가 표시되지 않습니다. 이런 식으로 뭔가에

collectionView.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: 50) 

:이 줄을 변경해보십시오

collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50)