Strange problem using UINib / pointer going AWOL

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]]; // These two should work equally well. But they don't... of course I'm using only one at a time ;) // THIS WORKS: UINib *test = [UINib nibWithNibName:@"NTDCell" bundle:nil]; NSArray *nibArray = [test instantiateWithOwner:self options:nil]; // THIS DOESN'T WORK: NSArray *nibArray = [cellNib instantiateWithOwner:self options:nil]; [[cell contentView] addSubview:[nibArray objectAtIndex:0]]; } return cell; } 

Thanks a lot!

+4
source share
1 answer

This line assigns an autorelease d instance of cellNib :

 cellNib = [UINib nibWithNibName:@"NTDCell" bundle:nil]; 

This means with the following line:

 NSArray *nibArray = [cellNib instantiateWithOwner:self options:nil]; 

... cellNib already freed when the associated auto-resource pool was exhausted, and its use would lead to undefined behavior.

If you want cellNib stay away, take responsibility for this, for example. using the declared property:

 self.cellNib = [UINib nibWithNibName:@"NTDCell" bundle:nil]; 
+7
source

All Articles