【问题标题】:UICollectionViewCell With Storyboard带有情节提要的 UICollectionViewCell
【发布时间】:2015-01-20 03:20:50
【问题描述】:

我在 Storyboard 中的 UICollectionViewController 中有一个 UICollectionView。 UICollectionViewController 链接到我的自定义class MasterViewController: UICollectionViewController, UICollectionViewDataSource, UICollectionViewDelegate,它的委托和数据源在情节提要中链接到这个类。

我在故事板中有一个原型 UICollectionViewCell,标识符为“MyCell”,来自我的自定义 class Cell: UICollectionViewCell

cellForItemAtIndexPath 方法中,应用程序在以下行崩溃:let cell:Cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as Cell

我不明白为什么。 registerClass:forCellWithReuseIdentifier:方法我没有实现,storyboard的标识符正好是“MyCell”,查了很多遍,delegate和datasource都链接到了正确的类。

当应用程序崩溃时,控制台中不会打印任何内容,只是“(lldb)”

这是我的代码:

class MasterViewController: UICollectionViewController,UICollectionViewDataSource,UICollectionViewDelegate {


var objects = [ObjectsEntry]()

@IBOutlet var flowLayout: UICollectionViewFlowLayout!

override func awakeFromNib() {
    super.awakeFromNib()
}


override func viewDidLoad() {
    super.viewDidLoad()

    flowLayout.itemSize = CGSizeMake(collectionView!.bounds.width - 52, 151)

}



// MARK: - Collection View

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return objects.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell:Cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as Cell

    return cell

}

【问题讨论】:

标签: ios swift storyboard uicollectionview uicollectionviewcell


【解决方案1】:

我遇到了同样的问题。 Raywenderlich Swift manual 帮助了我。我在这里复制MyCollectionViewController

  • 控制器和情节提要中的标识符必须匹配。
  • 创建自定义UICollectionViewCell 类。
  • 在情节提要中设置此UICollectionViewCell
  • 请勿致电viewDidLoad()
  • 请勿拨打registerClass:forCellWithReuseIdentifier:
  • collectionView:layout:sizeForItemAtIndexPath: 中使用UICollectionViewDelegateFlowLayout 设置单元格项目大小。

    import UIKit
    
    class MyCollectionViewController:
    UICollectionViewController,
    UICollectionViewDelegateFlowLayout {
    
    private let reuseIdentifier = "ApplesCell"
    
    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    override func collectionView(collectionView: UICollectionView,
               cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as MyCollectionViewCell
        cell.backgroundColor = UIColor.redColor()
        cell.imageView.image = UIImage(named: "red_apple")
        return cell
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 2015-09-08
    • 2012-03-27
    • 2012-12-24
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多