我正在尝试动态创建自定义UITableViewCell,以便在分组的UITableView中显示。该表放在IB中的UIViewController中,而自定义单元格在UITableView委托的tableView:cellForRowIndexAtPath方法中实例化。问题是加载视图控制器时,表格视图中都没有出现标签。 UICountingLabel是一个自定义的UILabel
我已经使用tableView注册了自定义实现:registerClass。我非常感谢任何帮助,并解释为什么会出现问题。
/**
* This is the custom cell for displaying
* the data for the enemy cell at the
* game over screen
* @df
*/
@implementation ADGameOverEnemyTableCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Create two labels, one for the name of the enemy and the
// other for displaying the number of kills, adding the autolayout constraints
bool isIphone = [[ADGameParameters sharedParameters] isIphone];
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.nameLabel = [[UILabel alloc] init];
[self.nameLabel setTextColor:[UIColor whiteColor]];
[self.nameLabel setFont:[UIFont fontWithName:@"Friday13" size:isIphone?15:20]];
[self.contentView addSubview:self.nameLabel];
self.killsLabel = [[UICountingLabel alloc] init];
[self.killsLabel setFormat:@"%d"];
[self.killsLabel setTextColor:[UIColor whiteColor]];
[self.killsLabel setFont:[UIFont fontWithName:@"Friday13" size:isIphone?15:20]];
[self.contentView addSubview:self.killsLabel];
}
return self;
}
- (void) layoutSubviews
{
// Local pointer for the VFL
UILabel *localNameLabel = self.nameLabel;
UICountingLabel *localKillsLabel = self.killsLabel;
// Layout contraints
NSDictionary *views = NSDictionaryOfVariableBindings(localNameLabel, localKillsLabel); // Greatest macro ever!!!
NSArray *verticalConstraintsForNameLabel =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[localNameLabel]|" options:0 metrics:nil views:views];
NSArray *verticalConstraintsForKillsLabel =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[localKillsLabel]|" options:0 metrics:nil views:views];
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[localNameLabel]-[localKillsLabel]|" options:0 metrics:nil views:views];
[self.contentView addConstraints:verticalConstraintsForNameLabel];
[self.contentView addConstraints:verticalConstraintsForKillsLabel];
[self.contentView addConstraints:horizontalConstraints];
[super layoutSubviews];
}
@end
下面是tableView的实现:forRowAtIndexPath按要求
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Calling the cellForRowAtIndexPath");
ADGameOverEnemyTableCell *cell = nil;
// Query the arrays for the data
switch ([indexPath section]) {
case ADStatsEnemy:
{
cell = [tableView dequeueReusableCellWithIdentifier:enemyIdentifier];
if (cell == nil)
{
cell = [[ADGameOverEnemyTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:enemyIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; // Only the weapons cells are selectable
NSLog(@"%@", indexPath);
NSArray *keys = [enemyData allKeys];
cell.nameLabel.text = [keys objectAtIndex:indexPath.row];
NSDictionary *enemyDataDict = [enemyData objectForKey:[keys objectAtIndex:indexPath.row]];
if ([enemyData isKindOfClass:[NSDictionary class]]){
NSInteger val = [[enemyDataDict objectForKey:@"Killed"] integerValue];
// cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",val];
[cell startKillsAnimationUpTo:val :4.0f];
}
else{
// cell.detailTextLabel.text = @"NO DATA, CHECK self.enemyData integrity";
cell.killsLabel.text = @"-1";
}
}
return cell;
break;
default:
break;
}
return nil;
}