Ios 为什么我不能在自定义单元格中使用标签
我使用的是自定义CollectionViewCell,当我想向自定义单元格中添加文本时,我很难做到这一点。在本教程中,我遵循的只是cell.label.text,但我在这里似乎无法做到这一点Ios 为什么我不能在自定义单元格中使用标签,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我使用的是自定义CollectionViewCell,当我想向自定义单元格中添加文本时,我很难做到这一点。在本教程中,我遵循的只是cell.label.text,但我在这里似乎无法做到这一点 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectio
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCollectionViewCell
// Configure the cell
cell.label.text = "Sec \(indexPath.section)/Item \(indexPath.item)" /////error here: CustomCollectionViewCell does not have the member label.
return cell
}
自定义CollectionViewCell的定义如下:
import UIKit
@IBDesignable
class CustomCollectionViewCell: UICollectionViewCell {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override init(frame: CGRect){
super.init(frame: frame)
setup()
}
func setup(){
self.layer.borderWidth = 1.0
self.layer.borderColor = UIColor.black.cgColor
self.layer.cornerRadius = 5.0
}
}
您需要在自定义单元格中创建
IBOutlet
的UILable
,然后才能在cellForItemAt
方法中访问那些lable
类似的cell.label
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lable: UILabel! //write this line and connect lable in storyboard.
}
更新:
打开故事板->选择自定义单元格->右面板->身份检查器->类名->在此处写入您的类名。您需要在自定义单元格中创建
IBOutlet
的UILable
,然后才能在cellForItemAt
方法中访问这些lable
像cell.label
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lable: UILabel! //write this line and connect lable in storyboard.
}
更新:
打开故事板->选择自定义单元格->右面板->标识检查器->类名->在此处写入您的类名。将您的CustomCollectionViewCell类指定给故事板选项中的单元格,然后您可以为标签创建一个出口
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lable: UILabel!
}
将CustomCollectionViewCell类指定给序列图像板选项中的单元,然后可以为标签创建一个插座
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var lable: UILabel!
}
是否在自定义单元格中创建了标签?是的,类设置为CustomCollectionViewCell。我更新了覆盖项您是否在自定义单元格中创建了标签?是的,类设置为CustomCollectionViewCell。我更新了覆盖。您需要在情节提要中添加CustomCollectionViewCell类,而不是在类中添加IBOutlet。您需要在情节提要中添加CustomCollectionViewCell类,而不是在类中添加IBOutlet。