如何在 CollectionView Cell 中显示字符串?
How to display string in CollectionView Cell?
我需要在 UICollectionViewCell
中显示一个字符串。我创建了一个自定义视图单元格。但是每当我 运行 应用程序时,它都会显示 "fatal error: unexpectedly found nil while unwrapping an Optional value"
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
}
来自我们在聊天中的讨论...
通过在 viewDidLoad 中执行此行,确保用于自定义单元格的 nib 文件与 collectionView 正确链接。
constCollectionView.register(UINib(nibName:"PlaceCollectionViewCell", bundle:nil), forCellWithReuseIdentifier: "Cell")
我需要在 UICollectionViewCell
中显示一个字符串。我创建了一个自定义视图单元格。但是每当我 运行 应用程序时,它都会显示 "fatal error: unexpectedly found nil while unwrapping an Optional value"
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
}
来自我们在聊天中的讨论...
通过在 viewDidLoad 中执行此行,确保用于自定义单元格的 nib 文件与 collectionView 正确链接。
constCollectionView.register(UINib(nibName:"PlaceCollectionViewCell", bundle:nil), forCellWithReuseIdentifier: "Cell")