自定义UiCollectionViewCell类init不被调用 [英] Custom UiCollectionViewCell class init not being called

查看:255
本文介绍了自定义UiCollectionViewCell类init不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,尽管我看不到要重新设置标签的大小,但我正在使用此代码来尝试调整文本的大小.同样,自定义类中的init方法也不会被调用.这是我的代码:

Hi I'm using this code to try and make the text resize though I can't seen to get the label to resize. Also the init method in the custom class is not being called. Here is my code:

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调用.我也尝试过在方法

What can I do to get the init to call so. I have also tried adjusting the text in the method

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath

尽管标签的文本大小没有变化, 这是带有长字符串的单元格的外观. 预先感谢您的帮助.

Though the text size of the label is not changing, Here's what the cell looks like with a longs string. Thanks in advance for your help.

如果我放置-

adjustsFontSizeToFitWidth

adjustsFontSizeToFitWidth

sizeToFit

sizeToFit

:

进入->

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

我也收到错误消息.

I also get an error.

推荐答案

将代码或断点放在init?(coder aDecoder: NSCoder)中.如果您在情节提要中添加了CollectionView,就会调用该方法.

Place your code or a breakpoint in init?(coder aDecoder: NSCoder). That should get called if you have your CollectionView added in your storyboard.

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

Update Handle your IBOutlets as so (in your cell subclass) because in the init function they may not be valid as yet, which I assume is your case:

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
}

这篇关于自定义UiCollectionViewCell类init不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆