1
votes

In a table view, I'm inserting cells with reuse identifier. So, I have to create one nib (xib) file for each cell. I want to put all the cell views in one xib file and get reference to them individually. How to do this?

3
why don't you access each cell using cellForRowAtIndexPath ? - Inder Kumar Rathore

3 Answers

1
votes

You can access xib as array of views, just like this :

-(UITableViewCell *) viewCellForSection:(NSInteger) section
{
    UITableViewCell *view = nil;

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

    switch ( section) {
        case 0: 
            view = (UITableViewCell*) [views objectAtIndex:1];
            break;
        case 1: 
            view = (UITableViewCell*) [views objectAtIndex:0];
            break;
        default: 
            view = (UITableViewCell*) [views objectAtIndex:2];
    }
    return view;
}
0
votes

you have to implement a code in the method (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. When you select cell in table, this method will be called. That's why you have to implement what you want.

I hope the following code will be helpful to you.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// yourObject is object of yourViewController and you can declare it .h file.

                    if (!yourObject) {
                    yourObject = [[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil];

                }

                UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
                self.navigationItem.backBarButtonItem = backBarButtonItem;
                [backBarButtonItem release];
                [self.navigationController pushViewController:yourObject animated:YES];

    }
0
votes

I know this is an old thread, but I actually found the response in the log when I tried to have multiple views in a nib for UITableViewCells. Here's the log:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'invalid nib registered for identifier (cellIdentifier) - nib must
contain exactly one top level object which must be a UITableViewCell instance'