Can I use viewDidLoad method in UITableviewCell?

IphoneIos

Iphone Problem Overview


Can I use viewDidLoad method in UITableviewCell?

Iphone Solutions


Solution 1 - Iphone

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

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

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

Note however that 'viewDidLoad' is called only once in the lifetime of the object; it is rather like an initializer in general OO programming. However, 'layoutSubviews' will be called many times on 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 do in layoutSubviews.

Note that viewDidLoad is called once only: layoutSubviews is called often.

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

Tutorial for custom cell

Solution 2 - Iphone

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

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

Solution 3 - Iphone

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

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

Solution 4 - Iphone

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

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.

Apple's documentation on the awakeFromNib() method:

> Summary > > Prepares the receiver for service after it has been loaded from an > Interface Builder archive, or nib file. > > Declaration > > func awakeFromNib() > > Discussion > The 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 and > action 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.

Solution 5 - Iphone

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

Solution 6 - Iphone

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;
     }
    

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.

Solution 7 - Iphone

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

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

Solution 8 - Iphone

YES you can write viewDidLoad in UITableViewCell but viewDidLoad is not delegate method of UITableViewCell so it will not called automatically so you have to call it manually by [self viewDidLoad] to execute your code.

Solution 9 - Iphone

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

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

Solution 10 - Iphone

If You are loading your UITableViewCell from XIB or Storyboard then initWithCoder() and awakeFromNib() methods are called(in the same order).

If you initialise your UITableViewCell programmatically then they won't be called.

Finally, prepareForReuse() will be called on a table view cell when it is being added to the pool of cells. In this method, you should reset the table view cell as if it were new (e.g. clean up text fields, labels, ...).

So I would suggest you to use awakeFromNib() method to setup your UITableViewCell.

For eg:-

class YourTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

       //do some work here that needs to happen only once, you don’t wanna change them later.
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5457View Question on Stackoverflow
Solution 1 - IphoneAbhishek SinghView Answer on Stackoverflow
Solution 2 - IphoneBerthier LemieuxView Answer on Stackoverflow
Solution 3 - Iphonesuperarts.orgView Answer on Stackoverflow
Solution 4 - IphonemarcelosalloumView Answer on Stackoverflow
Solution 5 - IphoneborrrdenView Answer on Stackoverflow
Solution 6 - IphoneghostatronView Answer on Stackoverflow
Solution 7 - IphonecodebendrView Answer on Stackoverflow
Solution 8 - IphoneMaulikView Answer on Stackoverflow
Solution 9 - IphoneShabeer AliView Answer on Stackoverflow
Solution 10 - IphoneVipul KumarView Answer on Stackoverflow