在集合视图的单元格中的图像

时间:2014-12-24 11:26:07

标签: ios swift

我在集合视图中显示来自解析后端的图像。

在CollectionViewController中我有这个工作代码:

class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

@IBOutlet var collection: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    println(numberOfDistinctValueInTypeOfStore)
    return numberOfDistinctValueInTypeOfStore
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 1
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {       
    let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell       
    // Configure the cell
    cell.backgroundColor = UIColor.redColor()     
    return cell
}

我想将图像添加到单元格中。 所以我将方法更改为此,应用程序崩溃:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {       
     let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell     
    // Configure the cell
    cell.backgroundColor = UIColor.redColor()     
    return cell
}

即使CellCollectionViewCell像这样是空的。它将崩溃而没有错误消息

class CellCollectionViewCell: UICollectionViewCell {   


}

通常情况下,我会在单元格内的imageview中添加一个插座。

错误在此代码中给出了断点

let cell:CellCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) 

作为CellCollectionViewCell

1 个答案:

答案 0 :(得分:0)

let cell:CellCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(**reuseIdentifier**, forIndexPath: indexPath)

它应该是一个字符串,您必须在故事板中设置此标识符。