Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 如何在UICollectionView单元格中设置标签?_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 如何在UICollectionView单元格中设置标签?

Ios 如何在UICollectionView单元格中设置标签?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我已经为此挣扎了几乎一整天了,找不到解决办法。我试图在视图控制器中创建一个UICollectionView,该视图控制器的每个单元格中只有一个标签。但是,无论我做什么,我似乎都无法将标签作为子视图添加到单元格中。以下是我在主视图控制器中的代码: var collectionView: UICollectionView! func configureCollectionView() { let layout: UICollectionViewFlowLayout =

我已经为此挣扎了几乎一整天了,找不到解决办法。我试图在视图控制器中创建一个
UICollectionView
,该视图控制器的每个单元格中只有一个标签。但是,无论我做什么,我似乎都无法将标签作为子视图添加到单元格中。以下是我在主视图控制器中的代码:

var collectionView: UICollectionView!

  func configureCollectionView()
    {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 50, height: 20)

        let y = interestsUnderline.frame.origin.y + 5
        let width = interestsUnderline.frame.width
        let frame = CGRect(x: 40, y: y, width: width, height: 30)
        collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)

        collectionView?.dataSource = self
        collectionView?.delegate = self
        collectionView?.register(InterestsCell.self, forCellWithReuseIdentifier: "InterestsCell")
        collectionView?.showsHorizontalScrollIndicator = false
        view.addSubview(collectionView!)
    }
我从ViewDidLayoutSubView调用configureCollectionView()。这是我在收藏视图单元格中的代码

    class InterestsCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()
    }

    let label: UILabel = {
        let label = UILabel()
        label.text = "Hello"
        label.textColor = .white
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    func setupViews() {
        contentView.backgroundColor = .red
        contentView.addSubview(label)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}

每当我将单元格出列时,我都会得到正确大小的单元格和正确的颜色。但是,当我在单元格的setupViews函数中设置标签上的约束时,视图控制器甚至不会完全打开。如果我不设置约束,标签就不存在了。我不知道在这一点上该做些什么,我希望能得到一些帮助。谢谢大家!

我查看了您上面的代码片段,发现您没有设置
框架
您需要设置
UILabel的
框架

let label: UILabel = {
            let label = UILabel()
            label.frame = CGRect(x: 40, y: 100, width: self.view.frame.width, height: 30)
            label.text = "Hello"
            label.textColor = UIColor.red
            label.translatesAutoresizingMaskIntoConstraints = false
            return label
        }()

希望这对您有所帮助

因为您的项目使用自动布局约束,您必须为标签设置这些约束:

func setupViews() {
    contentView.backgroundColor = .red
    contentView.addSubview(label)
    label.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
    label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
}

在InterestCell类中添加以下代码:

 override func layoutSubviews() {
        super.layoutSubviews()
        setupViews()
    }
而不是:

override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()
    }
required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

希望这能起作用

您需要为标签添加约束。显示您尝试执行此操作但无效的代码。@这里是label.centerXAnchor.constraint(equalTo:contentView.centerXAnchor)。isActive=true label.centerYAnchor.constraint(equalTo:contentView.centerYAnchor)。isActive=true@Maddyヅヅ 编辑:这不起作用,因为出于某种原因,我无法从该闭包访问self.view。不过我可以把它设为常数。但是,如果标签文本比单元格框宽,则部分文本将被截断。是否有任何方法可以动态更改单元格框架的大小?@AteethKosuri collectionView具有sizeForItemAtIndexPath委托您可以计算该框架上单元格的估计宽度/高度,并可以基于该@Maddy设置单元格的大小ヅヅ 当我将标签的帧设置为常量并使用sizeForItemAtIndexPath函数检查标签的宽度以便设置单元格宽度时,标签的宽度将保持为该常量,因此不会发生任何更改。