Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load xib file for cell in tableview in iphone

I saw there are lots of resources available on net regarding this question. I have to load a different XIB (UIViewContoller) file for my cell. I have designed my cell looks there and I want to load that design in my cell.

I wrote this code but I am getting an exception.

-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0
2011-07-11 14:42:27.461 TableViewTest[2776:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0'

And here is my code of loading nib file

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
    // Load the top-level objects from the custom cell XIB.
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"loadXibfile" owner:self options:nil];
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
    cell = [topLevelObjects objectAtIndex:0];
}


return cell;
like image 761
Ajay_Kumar Avatar asked Jul 11 '11 09:07

Ajay_Kumar


2 Answers

You should use UITableViewCell and not UIView in your XIB file.

like image 65
rckoenes Avatar answered Sep 28 '22 03:09

rckoenes


Here is the solution and it is useful for me and home for you or some else may be.

 cell = [[[YourCustomcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
        NSArray *toplavelobject=[[NSBundle mainBundle]loadNibNamed:@"YourCustomcell" owner:self options:nil];
        for(id c in toplavelobject)
        {
            if ([c isKindOfClass:[UITableViewCell class]])
            {
                cell=(YourCustomcell *) c;
                break;
            }
        }
like image 37
Muhammad Rizwan Avatar answered Sep 28 '22 02:09

Muhammad Rizwan