使用UINib /指针AWOL的奇怪问题

Toa*_*tor 4 iphone pointers objective-c nib

我在使用UINib时遇到了一些奇怪的事情,尽管我怀疑真正的问题可能会被埋没在其他地方.

我的应用程序正在使用tableview,由于它们的复杂性,已在Interface Builder中准备了单元格的内容.对于新单元(与重用单元相对),使用UINib类实例化nib的内容.因为每个单元只有一个nib用于减少每次加载文件的开销,所以我cellNib在我的viewcontroller子类中添加了一个UINib作为属性,我在viewDidLoad的实现中加载了一次.

现在是奇怪的部分.一切都工作正常,tableview填充了它的数据,所有单元格都设置了nib的内容.但是当我滚动tableview时,应用程序崩溃了.
callstack给出了这个:- [NSCFNumber instantiateWithOwner:options:]:发送到实例的无法识别的选择器 显然,再次从cellNib实例化内容的消息已被发送到错误的对象.发送消息的对象不时会有所不同,因此会随机发生一些事情.

我不明白 - 为什么在加载tableview时它会工作大约10次,但是当滚动tableview时它不再工作了?

如果我每次都创建一个新的UINib实例(如下面的代码所示),那么一切正常,包括滚动.

我在哪里弄错了?我的UINib属性的指针是否会变形?如果是这样,为什么?

这是我正在使用的代码(我删除了所有数据加载和其他东西,使其更容易阅读):

@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
Run Code Online (Sandbox Code Playgroud)

实施:

@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;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!!

Geo*_*che 7

此行将autoreleased实例分配给cellNib:

cellNib = [UINib nibWithNibName:@"NTDCell" bundle:nil];
Run Code Online (Sandbox Code Playgroud)

这意味着使用以下行:

NSArray *nibArray = [cellNib instantiateWithOwner:self options:nil];
Run Code Online (Sandbox Code Playgroud)

... cellNib当关联的自动释放池耗尽并且使用它时,已经解除分配将导致未定义的行为.

如果你想cellNib留下来拥有它,例如使用声明的属性:

self.cellNib = [UINib nibWithNibName:@"NTDCell" bundle:nil];
Run Code Online (Sandbox Code Playgroud)