今天在项目中使用UITableViewCell派生类,想到用Xib来实现。
在实现过程中碰到问题。如下
static NSString *CellIdentiferId = @"AccountTableViewCellId";
static BOOL ynibsRegistered = NO;
if (!ynibsRegistered)
{
UINib *nib = [UINib nibWithNibName:@"AccountTableViewCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellIdentiferId];
ynibsRegistered = YES;
}
AccountTableViewCell *cell = (AccountTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
if(!cell)
{
cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentiferId];
}
之前用这样的代码都没碰到过问题。
用最新Xcode 6,当UITableViewCell派生类中的成员,比如IBOutlet UILabel,生成的成员label对象为空,目前还不清楚是什么原因所致。
改进后的代码。
static NSString *CellIdentiferId = @"AccountTableViewCellId";
// static BOOL ynibsRegistered = NO;
// if (!ynibsRegistered)
// {
// UINib *nib = [UINib nibWithNibName:@"AccountTableViewCell" bundle:nil];
// [tableView registerNib:nib forCellReuseIdentifier:CellIdentiferId];
// ynibsRegistered = YES;
// }
AccountTableViewCell *cell = (AccountTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
if(!cell)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AccountTableViewCell" owner:self options:nil] ;
cell = [nib objectAtIndex:0];
// cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentiferId];
}