在UITable中显示自定义单元格

时间:2010-12-16 20:34:22

标签: iphone objective-c uitableview

我正在尝试在我创建的自定义单元格中显示我的数据,这些数据位于UITableView中。这是我的自定义单元类(CustomCell.m):

@implementation CustomCell

@synthesize primaryLabel, secondaryLabel, myImageView;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
   if (self == [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
     // Initialization code
     primaryLabel = [[UILabel alloc]init];
     primaryLabel.textAlignment = UITextAlignmentLeft;
     primaryLabel.font = [UIFont systemFontOfSize:14];
     secondaryLabel = [[UILabel alloc]init];
     secondaryLabel.textAlignment = UITextAlignmentLeft;
     secondaryLabel.font = [UIFont systemFontOfSize:8];
     myImageView = [[UIImageView alloc]init];
     [self.contentView addSubview:primaryLabel];
     [self.contentView addSubview:secondaryLabel];
     [self.contentView addSubview:myImageView]; 
    }
    return self;
  }

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;
    frame= CGRectMake(boundsX+10 ,0, 50, 50);
    myImageView.frame = frame;
    frame= CGRectMake(boundsX+70 ,5, 200, 25);
    primaryLabel.frame = frame;
    frame= CGRectMake(boundsX+70 ,30, 100, 15);
    secondaryLabel.frame = frame;
  }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}

然后,我创建了具有tableView的rootViewController,其中应显示自定义单元格(TeamsViewController.m):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
// Return the number of rows in the section.
    return 5;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


// Custom cell here
    switch (indexPath.row) {
     case 0:
      cell.primaryLabel.text =@"Meeting on iPhone Development";
      cell.secondaryLabel.text = @"Sat 10:30";
      cell.myImageView.image = [UIImage imageNamed:@"Icon4.png"];
      break;
     case 1:
      cell.primaryLabel.text = @"Call With Client";
      cell.secondaryLabel.text = @"Planned";
      cell.myImageView.image = [UIImage imageNamed:@"Icon4.png.png"];
      break;
     case 2:
      cell.primaryLabel.text = @"Appointment with Joey";
      cell.secondaryLabel.text = @"2 Hours";
      cell.myImageView.image = [UIImage imageNamed:@"Icon4.png.png"];
      break;
     case 3:
      cell.primaryLabel.text = @"Call With Client";
      cell.secondaryLabel.text = @"Planned";
      cell.myImageView.image = [UIImage imageNamed:@"Icon4.png.png"];
      break;
     case 4:
      cell.primaryLabel.text = @"Appointment with Joey";
      cell.secondaryLabel.text = @"2 Hours";
      cell.myImageView.image = [UIImage imageNamed:@"Icon4.png.png"];
      break;
     default:
      break;
    }

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
// Navigation logic may go here. Create and push another view controller.
    [self.navigationController pushViewController:self.teamsView animated:YES];
}

所以,问题是:数据没有显示在单元格中,我不明白为什么。这是目前看起来像的屏幕截图。

http://dl.dropbox.com/u/1481176/Screen%20shot%202010-12-16%20at%203.30.42%20PM.png

任何帮助将不胜感激!感谢。

1 个答案:

答案 0 :(得分:2)

您正在拨打initWithStyle:reuseIdentifier:而不是覆盖initWithFrame:reuseIdentifier:

相关问题