Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我不能在自定义单元格中使用标签_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 为什么我不能在自定义单元格中使用标签

Ios 为什么我不能在自定义单元格中使用标签,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我使用的是自定义CollectionViewCell,当我想向自定义单元格中添加文本时,我很难做到这一点。在本教程中,我遵循的只是cell.label.text,但我在这里似乎无法做到这一点 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectio

我使用的是自定义CollectionViewCell,当我想向自定义单元格中添加文本时,我很难做到这一点。在本教程中,我遵循的只是cell.label.text,但我在这里似乎无法做到这一点

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。