简体   繁体   English

UICollectionView正在显示,但单元格未显示在swift中

[英]UICollectionView is showing but the cells are not showing in swift

I created a collectionView programmatically (without storyboard). 我以编程方式创建了一个collectionView(没有storyboard)。 I set the background color to red, and it is showing properly. 我将背景颜色设置为红色,并且显示正确。 But the cells are not showing. 但细胞没有显示出来。 Does anyone know why the cells are not showing? 有谁知道为什么细胞没有显示?

private let cellId = "cellId"
class InfoViewShow: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellHeight:CGFloat = 80

var collectionView: UICollectionView?
let layout = UICollectionViewFlowLayout()

override init() {
    super.init()

    setupCollectionview()

    collectionView?.registerClass(InfoCell.self, forCellWithReuseIdentifier: cellId)
}

private func setupCollectionview() {
    if let view = UIApplication.sharedApplication().keyWindow {
        //let y = (view.frame.width * 10 / 16) + 34 + 26
        collectionView = UICollectionView(frame: .zero , collectionViewLayout: layout)
        collectionView?.backgroundColor = UIColor.redColor()
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.frame = CGRectMake(0, 0, view.frame.width, view.frame.height) // y to y
    }
}

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
    cell.backgroundColor = UIColor.blackColor()
    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(collectionView.frame.width, cellHeight)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(5, 5, 5, 5); //top,left,bottom,right
}
}

I dont' see you add collectionView to view. 我没有看到你添加collectionView来查看。

if let view = UIApplication.sharedApplication().keyWindow {
        //let y = (view.frame.width * 10 / 16) + 34 + 26
        collectionView = UICollectionView(frame: .zero , collectionViewLayout: layout)
        collectionView?.backgroundColor = UIColor.redColor()
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.frame = CGRectMake(0, 0, view.frame.width, view.frame.height) // y to y

        ///try add
        self.view.addSubview(collectionView)
    }

You must implement numberOfSectionsInCollectionView of UICollectionViewDataSource delegate method. 您必须实现UICollectionViewDataSource委托方法的numberOfSectionsInCollectionView This method is optional but you must implement it and return at least one section. 此方法是可选的,但您必须实现它并返回至少一个部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM