今天在項目中使用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];
}