Abdullah Umer
Abdullah Umer

Reputation: 4624

Custom UITableViewCell init

I have created a custom UITableViewCell using storyboards.

I am loading it like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJAddCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addCell"];
    // setting properties of cell 
    return cell;
}

Now I need to add some more customization programmatically during initialization. But the problem is that in my HJAddCell, the init methods are not called. What should I do so that the the init method is called or is there any other way to add more customization code to the custom cell class at init time with minimum effort.

Update: I need to add borders to UILabels. I didn't find any thing to do this in storyboard.

Upvotes: 3

Views: 10547

Answers (3)

Kreiri
Kreiri

Reputation: 7850

UIViews have two designated initializers: initWithFrame: and initWithCoder:. initWithCoder: is used if view is loaded from nib/storyboard, initWithFrame - if view is created programmatically.

Upvotes: 13

J2theC
J2theC

Reputation: 4452

Of course the init methods are not being called. You are not calling any init methods yourself. If you are registering HJAddCell's nib with the table view, the initialization method being called is initWithCoder. If you are using a nib, you should also ask yourself about why do you need to customize a custom UITableViewCell on the runtime. I mean, it's already a custom cell. You should create the cell with the properties you need to have.

Upvotes: 5

talanb
talanb

Reputation: 994

How about adding a new method to HJAddCell called reset? Move the initialization code that needs to happen before each use into the reset method and call reset from your initializer.

Then you would call the reset method after dequeue-ing your cell.

Upvotes: 0

Related Questions