ios - UICollectionView 正在显示,但单元格未快速显示

标签 ios swift2 uicollectionview uicollectionviewcell uicollectionviewlayout

我以编程方式创建了一个 collectionView(没有 Storyboard)。我将背景颜色设置为红色,并且显示正常。但是细胞没有显示出来。有谁知道为什么细胞不显示?

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
}
}

最佳答案

我没看到你添加 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)
    }

关于ios - UICollectionView 正在显示,但单元格未快速显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39461748/

相关文章:

iphone - UISearchBar 与 iMac Finder 搜索功能相同

ios updateDisplay 定时器

ios - 如何在 iOS 项目中正确配置 Realm DB 文件 (<db_name>.realm) 的路径?

ios - Swift 3 中的 PBEWithMD5AndDES 加密

iOS - 对 View 中的多个 Tableview 或 Collectionview 使用/传递手势识别器(Swift)

ios - 访问扩展中项目中存在的图像

objective-c - MKNetworkKit 和 GCD dispatch_group_t

ios - 无法分配给受限于类的协议(protocol)中的属性 - Swift 编译器错误

ios - 在单元格 UICollectionView 中插入/删除部分

ios - UITableView with UICollectionView in row weird scroll in different rows at the same time