ios 我可以在 UITableviewCell 中使用 viewDidLoad 方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10578246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:22:04  来源:igfitidea点击:

Can I use viewDidLoad method in UITableviewCell?

iphoneios

提问by user5457

Can I use viewDidLoadmethod in UITableviewCell?

我可以使用viewDidLoad方法UITableviewCell吗?

回答by Abhishek Singh

No you don't write viewDidLoad in Custom cell class subclassing UITableViewCell(It;s part of UIViewController) .you have a method called

不,您不会在自定义单元格类中编写 viewDidLoad 子类化 UITableViewCell(It;s part of UIViewController) 。您有一个名为的方法

-(void)layoutSubviews
{
    [super layoutSubviews];
}

where in you can define frames and all for custom cell's controls.Refer Apple's UITableviewCell reference

您可以在其中为自定义单元格的控件定义框架和所有内容。请参阅 Apple 的 UITableviewCell 参考

Note howeverthat 'viewDidLoad' is called only oncein the lifetime of the object; it is rather like an initializer in general OO programming. However, 'layoutSubviews' will be called many timeson each cell (depending on issues like scrolling and so on). It's important to realize that for this reson many of the things you "usually do" in viewDidLoad, you can not doin layoutSubviews.

但是请注意,'viewDidLoad'在对象的生命周期中被调用一次;它更像是一般面向对象编程中的初始化程序。但是,“layoutSubviews”在每个单元格上多次调用(取决于滚动等问题)。重要的是要意识到,对于这个原因,您在 viewDidLoad 中“通常做”的许多事情,您不能在 layoutSubviews 中

Note that viewDidLoad is called onceonly: layoutSubviews is called often.

请注意, viewDidLoad 只调用一次: layoutSubviews经常调用。

It will just be a simple function if you write it.

如果您编写它,它将只是一个简单的函数。

Tutorial for custom cell

自定义单元格教程

回答by Berthier Lemieux

If you're trying to initialise some IBOutlets of your UITableViewCell, you may want to go for the awakeFromNib() call:

如果您尝试初始化 UITableViewCell 的某些 IBOutlets,您可能需要使用awakeFromNib() 调用:

override func awakeFromNib() {
    super.awakeFromNib()
    // Do your stuff
    // myIBOutlet.text = "Hello" 
}

回答by superarts.org

Better to call super.layoutSubviews()as well. Swift syntax:

最好也打电话super.layoutSubviews()。快速语法:

override func layoutSubviews() {
    super.layoutSubviews()
}

回答by marcelosalloum

An awakeFromNib()is similar to a viewDidLoad(), thus it will run less often and must be called for initialization commands.

AnawakeFromNib()类似于 a viewDidLoad(),因此它的运行频率较低,并且必须为初始化命令调用。

A layoutSubviews()is similar to a viewWillAppear(), so it should be called when you want to change something in the view presentation. For instance, when a user changes the phone orientation and the app needs to update some of the view's specific configuration, like size, colour and position.

AlayoutSubviews()类似于 a viewWillAppear(),因此当您想更改视图表示中的某些内容时应该调用它。例如,当用户更改手机方向并且应用程序需要更新某些视图的特定配置时,例如大小、颜色和位置。

Apple's documentation on the awakeFromNib()method:

Apple 关于该awakeFromNib()方法的文档:

Summary

Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

Declaration

func awakeFromNib()

DiscussionThe nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet andaction connections already established.

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.

概括

从 Interface Builder 存档或 nib 文件加载接收器后,为接收器准备服务。

宣言

func awakeFromNib()

讨论nib 加载基础结构向从 nib 存档重新创建的每个对象发送一个awakeFromNib 消息,但前提是存档中的所有对象都已加载并初始化。当一个对象接收到一个awakeFromNib 消息时,它保证已经建立了它的所有出口和动作连接。

您必须调用awakeFromNib 的超级实现,让父类有机会执行它们需要的任何其他初始化。尽管此方法的默认实现什么都不做,但许多 UIKit 类提供了非空实现。您可以在自己的awakeFromNib 方法中的任何时候调用super 实现。

回答by ghostatron

I realize this is an old thread, but a pattern I sometimes use (especially if I have a base class for my custom cells):

我意识到这是一个旧线程,但我有时会使用一种模式(特别是如果我的自定义单元格有一个基类):

  1. Add a BOOL property, call it something like "cellAlreadyDidLoad". I named it that way because BOOL will default to NO, so I don't have to worry about setting it in an init method.

  2. Create a cellDidLoad method, named so with the intent that it's kind of like the viewDidLoad that we all know and love.

  3. Override the layoutSubviews method and add:

    if (!self.cellAlreadyDidLoad)
    {
        [self cellDidLoad];
        self.cellAlreadyDidLoad = YES;
    }
    
  1. 添加一个 BOOL 属性,将其命名为“cellAlreadyDidLoad”。我这样命名是因为 BOOL 将默认为 NO,所以我不必担心在 init 方法中设置它。

  2. 创建一个 cellDidLoad 方法,命名如此,目的是它有点像我们都知道和喜爱的 viewDidLoad。

  3. 覆盖 layoutSubviews 方法并添加:

    if (!self.cellAlreadyDidLoad)
    {
        [self cellDidLoad];
        self.cellAlreadyDidLoad = YES;
    }
    

Now you have a method, cellDidLoad, that will only be called once, knowing that the UI elements (including your IBOutlets) will be present. Very much like viewDidLoad on a view controller.

现在您有一个方法 cellDidLoad,它只会被调用一次,知道 UI 元素(包括您的 IBOutlets)将出现。非常像视图控制器上的 viewDidLoad。

回答by codebendr

If your UITableViewCell has a variable then you can use didSet like so

如果您的 UITableViewCell 有一个变量,那么您可以像这样使用 didSet

var stickers:[Sticker]? {
    didSet {
        if let stickers = stickers {
            //do some initialisation 
        }
    }
}

回答by borrrden

The UIKit function viewDidLoadis a member of UIViewController, and UITableViewCell does not inherit from it. You can write a method namedviewDidLoadif you desire, but it will not be called like it is in a UIViewController subclass.

UIKit 函数viewDidLoad是 UIViewController 的成员,UITableViewCell 不继承自它。您可以根据需要编写一个命名的方法viewDidLoad,但不会像在 UIViewController 子类中那样调用它。

回答by Maulik

YES you can write viewDidLoadin UITableViewCellbut viewDidLoadis not delegate method of UITableViewCellso it will not called automatically so you have to call it manually by [self viewDidLoad]to execute your code.

是,你可以写viewDidLoadUITableViewCell,但viewDidLoad没有委托的方法,UITableViewCell所以,所以你必须手动调用它通过它不会自动调用 [self viewDidLoad]执行代码。

回答by Shabeer Ali

you can use initWithFrame for UITableViewCell or UICollectionViewCell or to any Subclass of UIView.

您可以将 initWithFrame 用于 UITableViewCell 或 UICollectionViewCell 或 UIView 的任何子类。

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       // Initialisation code here
    }
    return self;
}