user210504
user210504

Reputation: 1759

How to add a custom subview to a UITableViewCell derived class when the UITableViewCell is loaded using loadNIB

I am trying to add a custom view control to a custom UITableViewCell which I had designed in the interface builder.

Now, to load UITableCellView I am using

NSArray * loadedViews = [[NSBundle mainBundle] loadNibNamed:@"CustomSearchResultsCell" owner:self options:nil];

What method of the CustomSearchResultsCell class will be called the Nib loads the view and initializes it. I tried using viewDidLoad, but UITableViewCell does not respond to this method. Also initWithStyle is not being called in this case.

TIA Nitin

Upvotes: 0

Views: 1345

Answers (2)

Roman Busygin
Roman Busygin

Reputation: 543

Also, take a look at Apple' Sample Code called AdvancedTableViewCells. In this example they use following code to load table view cell from XIB:

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

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
        cell = tmpCell;
        self.tmpCell = nil;     
    }

    return cell;
}

Upvotes: 0

Stefan Arentz
Stefan Arentz

Reputation: 34945

Views loaded from a nib are initialized with initWithCoder:, which you can implement in a similar way like initWithFrame:.

Upvotes: 3

Related Questions