Michael Campsall
Michael Campsall

Reputation: 4335

How to properly Init a custom UITableviewCell?

I am using the following 2 methods to return a custom cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *key = [self keyForIndexPath:indexPath];
    UITableViewCell *cell;

    if ([key isEqualToString:DoneButtonCellKey]) {
        cell = [self [self doneButtonCellForIndexPath:indexPath];
        return cell;
    } else {
        //code to return default cell...
    }
} 

Then:

- (DoneButtonCell *)doneButtonCellForIndexPath: (NSIndexPath *)indexPath {

    DoneButtonCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DoneButtonCellIdentifier forIndexPath:indexPath];
    return cell;

}

What is the correct init method to use with the cell here so I can change some properties of the cell when it is initialized?

EDIT: I found the problem, as the init/awakeFromNib methods were not being called for me. I tracked down the error and it was that I had not changed the "Custom Class" from UITableViewCell to my custom class. Now awakeFromNib AND initWithCoder work as described below.

Upvotes: 33

Views: 48074

Answers (4)

Mostafa Berg
Mostafa Berg

Reputation: 3239

You can do your changes inside the DoneButtonCell's class, either in the

- (void)awakeFromNib
{
 .. essential to call super ..
 super.awakeFromNib()
 //Changes done directly here, we have an object
}

Or the initWithCoder: method:

-(id)initWithCoder:(NSCoder*)aDecoder
{
   self = [super initWithCoder:aDecoder];

   if(self)
   {
     //Changes here after init'ing self
   }

   return self;
}

Upvotes: 37

TALE
TALE

Reputation: 1060

If you're using Swift, remember the easy way to ensure a view is initialized when it is created is to use the didSet method. For example, to make a UIImageView into a round shape you could add code like this:

@IBOutlet weak var profileImageView: UIImageView! {
    didSet {
        // Make the profile icon circle.
        profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
        profileImageView.clipsToBounds = true
    }
}

Upvotes: 6

Ayan Sengupta
Ayan Sengupta

Reputation: 5386

  1. First try to dequeue a cell if possible using dequeueReusableCellWithIdentifier method of UITableView.
  2. If cell is not available (nil) use [[NSBundle mainBundle] loadNibNamed:@"<#your custom cell nib name#>" owner:nil options:nil][0] to initialize it.
  3. In your custom cell's .m file, implement initWithCoder: initializer for custom initialization code:

- (id)initWithCoder:(NSCoder *)aDecoder {  
    self = [super initWithCoder:aDecoder];  
    //your custom initialization code  
    return self;  
}  

This is the designated initializer that is called when any view is loaded from a nib with loadNibNamed, like a custom table view cell.

Upvotes: 1

Duncan Groenewald
Duncan Groenewald

Reputation: 8988

This is how I am initialising custom cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"FileTableViewCell";

    FileTableViewCell *cell = (FileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FileTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Configure the cell here...
      // Configure the cell.
FileRepresentation* fileRepresentation = _fileList[indexPath.row];
cell.textLabel.text = [self userFilename:[fileRepresentation.fileName stringByDeletingPathExtension]];

cell.detailTextLabel.text = [fileRepresentation modifiedDate];


cell.accessoryView=nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.progressIndicator setHidden:YES];

cell.imageView.image = [UIImage imageNamed:_fileImageName];

// Disable any user interaction while processing a request
if (_fileIsOpen || _creatingDocument || _deletingDocument) {

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor grayColor];

} else {

    cell.textLabel.textColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;

}

}

Upvotes: 3

Related Questions