I have something weird about using UINib, although I suspect that the real problem might be buried elsewhere.
My application uses tableview, the content for the cells was prepared in Interface Builder due to their complexity. For new cells (as opposed to reusable ones), nib content is created using the UINib Class . Since only one nib is used for all cells, and each time to reduce the overhead of downloading a file, I added UINib as a cellNib property to my viewcontroller subclass, which is loaded once in my viewDidLoad implementation.
Now for the weird part. Everything works fine, the tableview is populated with its data, and all cells are configured with the contents of the nib, as it should be. But as soon as I scroll through the table, the application crashes.
The column gave this: - [NSCFNumber instantiateWithOwner: options:]: unrecognized selector sent to the instance Obviously, the message to create content from cellNib was again sent to the wrong object. The object that the message is sent to varies from time to time, so something random happens.
I donβt understand why it works about 10 times when loading a tableview, but no more when the table view scrolls?
If I create a new instance of UINib each (as shown in my code below), everything works fine, including scrolling.
Where can I be wrong? Is a pointer to my UINib property swellable? If so, why?
Here is the code I'm using (I removed all data loading and other things to make it easier to read):
@interface NTDPadViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { NSManagedObjectContext *managedObjectContext; NSMutableArray *ntdArray; IBOutlet UITableView *ntdTableView; UINib *cellNib; } @property(nonatomic,retain) NSManagedObjectContext *managedObjectContext; @property(nonatomic,retain) NSMutableArray *ntdArray; @property(nonatomic,retain) UITableView *ntdTableView; @property(nonatomic,retain) UINib *cellNib; @end
Implementation:
@implementation NTDPadViewController @synthesize managedObjectContext; @synthesize ntdArray; @synthesize ntdTableView; @synthesize cellNib; -(void)viewDidLoad { [super viewDidLoad]; cellNib = [UINib nibWithNibName:@"NTDCell" bundle:nil]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setBackgroundColor:[UIColor clearColor]];
Thanks a lot!