UITableView and its cell in a xib file

13,804

You Should register the nib as follows.

[self.tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"MyCell"]

I have generally seen that registerClass doesn't load the view, but only the class. I dont know why.

Also one .xib should contain only one view. So you have to make a different for UITableViewCell and UITableView. The reason behind is very simple. Each time the xib is loaded, all the view inside the xib are created. You wouldn't want a new table each time you create a cell. Right?

As for the crash you are facing. The error looks like there is an outlet issue with name tableView. Try removing outlets and re initializing them

Additions

for a UICollectionView you can register a class, it would load your view from xib.

 [self.collectinView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier]

Update

If you are using a storyboard, you can just add two prototype cells and that will add two cells within the tableview. You can add all views right there and give different class files to them. Would reduce the number of xib files you need to maintain and also you will not require to register your nibs. This could be a tutorial for it

Share:
13,804
Eddie
Author by

Eddie

I'm an iOS developer, working mainly on Swift and Objective-C. Still learning. 2020 Still working as an iOS developer. Experienced. 2015 I'm now a developer working in an e-commercial open solutions company. My project is based on Objective-c, mainly develop an iPad app for supporting Magento solution. 2013 I'm a graduate student in software developing, now working for a Japanese company. Work base on java, C#, SQLServer, MySQL, objective-C, ruby, ruby on rails.

Updated on July 03, 2022

Comments

  • Eddie
    Eddie almost 2 years

    Long story short, I want to create a XIB file, where it contains:

    1. A view. In that view, it has many things and a tableView. (connected data source, delegate to file's owner). That view is the main view of the XIB files.

    2. Custom cell, (use as the prototype cell of tableView, since XIB doesn't support prototype tableView).

    How could I achieve both of these? Also, I want to know the same apply to UICollectionView.

    I had created both of those, load it to the main view (the main view is in the main storyboard). But the tableView doesn't load the custom cell as expected. The cell is just blank, doesn't look like what it's designed in the nib.

    EDIT 1

    Here's how I register the nib to the main view in storyboard:

    - (UIView *)buildViewFromViewControllerClass:(Class)viewControllerClass
    {
        id viewController = [[viewControllerClass alloc] initWithNibName:NSStringFromClass(viewControllerClass) bundle:nil];
        [viewController view].frame = self.containerView.bounds;
    
        [self addChildViewController:viewController];
    
        return [viewController view];
    }
    

    I have many viewControllers, so I figure out that this is the best way to add VC.

    About the nib in description above, I just use regular initialization:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // init cell
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
        if (!cell) {
            cell = [[PaymentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
        }
    
        return cell;
    }
    

    If in the view did load method, I register the nib to the tableView, it'll crash in [tableView dequeueResuableCell...]. The registered code:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    //    [self.tableView registerNib:[UINib nibWithNibName:self.nibName bundle:nil]
    //                forCellReuseIdentifier:@"CustomCell"];
    // I'm commenting it, because it caused crash.
    }
    

    Crash message:

    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'

    What did I do wrong here? Here's a picture how I designed the nib:

    Model

    As you can see, the file's owner's mainView is connected to the mainView, where it contain the tableView. The customCell is just a custom designed cell, define in the same file (the .m file).

  • Eddie
    Eddie almost 9 years
    I'm very disappoint to say this: It actually work if I separate the view into different XIB files. It doesn't what I desired in the first place. Let's see if there's any better answer. Also, please confirm if this method should work for collectionView too?
  • Ganesh Somani
    Ganesh Somani almost 9 years
    About a better answer i don't know... but I feel this is the most correct one... You can't have two views in xib, it just isn't efficient from memory usage point of view