【问题标题】:UICollectionView is showing but the cells are not showing in swiftUICollectionView 正在显示,但单元格未快速显示
【发布时间】:2016-09-13 03:24:01
【问题描述】:

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

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

【问题讨论】:

  • 经常出现的问题是数据源和delegate没有设置或者size太小,加断点看看是否调用了cellForItem。您可以尝试将这些方法导出到扩展,有时它可以工作
  • 我尝试在两个 cellforitems 和 numberofitems 上放置一些断点,但它们都没有被调用。还有什么建议吗?
  • 尝试初始化大小大于零的集合视图
  • 我做到了,我得到了相同的结果。细胞仍然没有显示。 collectionView = UICollectionView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height), collectionViewLayout: layout) UICollectionView 显示,红色背景,因为我将颜色更改为红色,但不是单元格.我认为根本没有调用数据源和委托
  • help.apple.com/xcode/mac/8.0/#/devda5478599 在运行时按下“调试视图图”按钮。检查左边的视图。以及右侧面板上的属性。祝你好运

标签: ios swift2 uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:

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

【讨论】:

  • 我将它添加到我拥有的辅助函数中 let infoView = InfoViewShow() barberProfileView.addSubview(infoView.collectionView!)
  • @ralph007 使用调试您的代码运行到if let view = UIApplication.sharedApplication().keyWindow { ?
  • 其实我有。这就是我访问keyWindow的方式。我觉得委托方法根本没有被调用。因为我在它们每个内部都放了一些打印语句用于调试目的,并且当我在它们上放置断点时它们没有被打印,也没有被调用
【解决方案2】:

您必须实现numberOfSectionsInCollectionViewUICollectionViewDataSource 委托方法。 此方法是可选的,但您必须实现它并至少返回一个部分。

【讨论】:

  • 我刚试过了,还是不行。我在委托函数上放了断点,我意识到根本没有被调用
  • 在 viewDidLoad 方法中尝试 'self.collectionView.reloadData()'
  • 它没有解决问题。集合视图显示为红色背景,因为我将背景颜色设置为红色,因此我可以更好地看到它,但看不到单元格。我认为根本没有调用数据源和委托函数
猜你喜欢
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多