在UICollectionViewController中显示单元格

时间:2016-03-28 16:49:52

标签: ios swift uicollectionview xcode7 uicollectionviewcell

我尝试制作一个UICollectionViewController,我可以在其中显示每个单元格的图像。当我想打开这个ViewController时,它会显示一个错误

import UIKit

private let reuseIdentifier = "Cell"

class RodelCollectionViewController: UICollectionViewController {

var personService: PersonService!

override func viewDidLoad() {
    super.viewDidLoad()
    assert(personService != nil, "Person Service has to be set, otherwise this class can't do anything useful.")
    // 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.
}

// MARK: - Table view data source

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

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

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonCollectionCell", forIndexPath: indexPath)

    if let rodelCollectionViewCell = cell as? RodelCollectionViewCell {
        rodelCollectionViewCell.personView?.person = personService.allPersons()[indexPath.item]
    }

    return cell
}

// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if let PersonDetailViewController = segue.destinationViewController as? PersonDetailViewController,
        let person = (sender as? RodelCollectionViewCell)?.personView?.person {
        PersonDetailViewController.person = person
    }
}

这是错误/Users/ritscher/Desktop/Bildschirmfoto 2016-03-28 um 13.15.42.png

我已经尝试了很多修复它,但总是向我显示同样的错误。我不知道在哪里解决这个问题

2 个答案:

答案 0 :(得分:3)

您是否已将单元格标识符(" PersonCollectionCell")分配给xib文件或故事板中的单元格?

我注意到您声明private let reuseIdentifier = "Cell"用于注册单元格。但是,当您将单元格出列时,您正在使用不同的reuseIdentifier "PersonCollectionCell"

此外,

我不建议在里面使用函数personService.allPersons()

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

每次单元格重用/出列时都会调用此方法,并且可能在将来引发性能问题。相反,我会将结果保存在数组中,并在每次更改时更新它,并且可以影响personService.allPersons()返回的内容。

我会声明一个像这样的懒惰变量:

private lazy var allPersons: [WhateverTheTypeIs] = {
    let allPersons = self.personService.allPersons()
    return allPersons
}

并且在collectionView数据源方法中使用allPersons而不是方法本身。

希望这有帮助。

答案 1 :(得分:1)

您的代码中的另一个问题位于

 self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

在这里,您尝试注册默认的UICollectionViewCell并在cellForItemAtIndexPath中尝试检查

if let rodelCollectionViewCell = cell as? RodelCollectionViewCell {
    rodelCollectionViewCell.personView?.person = personService.allPersons()[indexPath.item]
}

在此代码中,您将检查自定义单元格此单元格如何成为自定义单元格

如果您想注册并创建自定义单元格,您应该是这样的:

viewDidLoad()

self.collectionView!.registerClass(RodelCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

cellForItemAtIndexPath

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! RodelCollectionViewCell

默认单元格
如果您想保留默认单元格,您的代码将保持与当前相同但不会进入自定义单元格的条件 单元格可能显示为空 如果你在cellforrow

中没有做任何其他事情

<强>更新 将这两个代码放在cellForItemAtIndexPath中 要更改单元格背景颜色

cell.contentView.backgroundColor = UIColor.redColor()

由于人们观点暂时为零,因为测试目的我们可以添加一个示例视图

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonCollectionCell", forIndexPath: indexPath)

    if let rodelCollectionViewCell = cell as? RodelCollectionViewCell {
        rodelCollectionViewCell.personView?.person = personService.allPersons()[indexPath.row]
    }
   cell.contentView.backgroundColor = UIColor.redColor()
   let lbl = UILabel(frame:CGRectMake(0,0,100,21))
   lbl.text = "\(indexPath.row)" //replace this value with your original value if it displays for the first time
   cell.contentView.addSubview(lbl)

    return cell
}
相关问题