gpt4 book ai didi

ios - 未调用自定义 UiCollectionViewCell 类初始化

转载 作者:行者123 更新时间:2023-12-01 18:07:41 28 4
gpt4 key购买 nike

嗨,我正在使用此代码尝试调整文本大小,尽管我看不到要调整标签的大小。自定义类中的 init 方法也没有被调用。这是我的代码:

class Cell: UICollectionViewCell {


@IBOutlet weak var label: UILabel!


override init(frame: CGRect) {
super.init(frame: frame)
print("init—>Not being called???\n")
self.label.adjustsFontSizeToFitWidth = true
self.cell.label.sizeToFit()
}

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


}

我该怎么做才能让 init 如此调用。我也尝试过调整方法中的文本
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath

虽然标签的文本大小没有改变,
这是带有 longs 字符串的单元格的样子。
enter image description here
在此先感谢您的帮助。

如果我放置 -

adjustsFontSizeToFitWidth



或者

sizeToFit



:

进入->
 required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.label.adjustsFontSizeToFitWidth = true // causes error
self.label.sizeToFit()
}

enter image description here
我也得到一个错误。

最佳答案

将您的代码或断点放在 init?(coder aDecoder: NSCoder) 中.如果您在 Storyboard 中添加了 CollectionView,则应该调用它。

更新
像这样处理您的 IBOutlets (在您的单元子类中),因为在 init 函数中它们可能尚未有效,我认为这是您的情况:

override func layoutSubviews() 
{
super.layoutSubviews()
self.label.adjustsFontSizeToFitWidth = true
self.label.minimumScaleFactor = 0.8 //set the scale factor or minimumFontSize so that it can then start adjusting the font size.
Self.label.numberOfLines = 1
}

关于ios - 未调用自定义 UiCollectionViewCell 类初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37957262/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com