如何在CollectionView Cell中显示字符串?

时间:2016-12-12 07:02:53

标签: ios uicollectionview swift3 uicollectionviewcell

我需要在UICollectionViewCell中显示一个字符串。我创建了一个自定义视图单元格。但每当我运行应用程序时,它会显示“致命错误:在解开可选值时意外发现nil”

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell

    cell.layer.borderWidth = 0.5
    cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor

   //cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor

    cell.layer.cornerRadius = 35
    cell.layer.masksToBounds = true

    //cell.placeLabel.text = places[indexPath.row] as! String
    cell.placeLabel.text = places[indexPath.row]

    return cell
}

1 个答案:

答案 0 :(得分:1)

从我们在聊天中的讨论......

通过在viewDidLoad中执行此行,确保用于自定义单元格的nib文件与collectionView正确链接。

constCollectionView.register(UINib(nibName:"PlaceCollectionViewCell", bundle:nil), forCellWithReuseIdentifier: "Cell")