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?
Asked
Active
Viewed 236 times
1
-
why don't you access each cell using `cellForRowAtIndexPath` ? – Inder Kumar Rathore Aug 31 '12 at 10:56
3 Answers
1
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;
}

tone303
- 11
- 1
-
1Ya, the views can be accessed like this or even with tag, but I want to use them in registerNib:forCellReuseIdentifier: method which takes UINib argument only. So any way to create UINib with one of the views in a nib? – user1559227 Aug 31 '12 at 11:19
0
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];
}

Prasad G
- 6,702
- 7
- 42
- 65
0
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'

Christian Di Lorenzo
- 3,562
- 24
- 33