将UILabel添加到自定义UITableViewCell

时间:2010-10-19 04:41:48

标签: iphone uikit

我正在使用uitableview中的自定义单元格并尝试在单元格中添加uilabels但无法在其上设置文本。以下是我如何尝试添加单元格以及我如何添加文本

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];    
if (cell == nil) {

    cell = [[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    cell.imageView.image = [UIImage imageNamed:@"iTeam.png"];

}
cell.orangeText1 = @"Mikes Bar";
cell.orangeText1 = @"4.3 mi northeast";
cell.whiteText1 = @"Level";
cell.whiteText1 = @"Freshman";

return cell;

}

我的自定义单元格类:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    // Initialization code
    //

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
    view.backgroundColor = [UIColor blackColor];
    view.opaque = YES;
    self.backgroundView = view;
    [view release];

    cellImage = [[UIImageView alloc] init];

    myLabel *lblview = [[myLabel alloc] initWithFrame:CGRectMake(self.imageView.frame.size.width+10, 0, 320, 20) OrangeText:orangeText1 WhiteText:whiteText1];
    [self.contentView addSubview:lblview];
    [lblview release];

    lblview = [[myLabel alloc] initWithFrame:CGRectMake(self.imageView.frame.size.width+10, 22, 320, 20) OrangeText:orangeText2 WhiteText:whiteText2];
    [self.contentView addSubview:lblview];
    [lblview release];

    UIImage *image = [UIImage imageNamed:@"acessory.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    self.accessoryView = button;
    [button release];

    UIImageView *imagevw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 8)];
    imagevw.image = [UIImage imageNamed:@"linee.png"];
    [self addSubview:imagevw];
    [imagevw release];

}

return self;

}

其他一切工作正常,但我没有在标签上看到文字。标签本身是从另一个uiview类调用的。

由于

3 个答案:

答案 0 :(得分:0)

将标签上的文字设为

cell.orangeText1.text = @"Mikes Bar";
cell.orangeText1.text = @"4.3 mi northeast";
cell.whiteText1.text = @"Level";
cell.whiteText1.text = @"Freshman";

答案 1 :(得分:0)

正如Gyani所说,但是你可以通过写它来使它更像ObjectiveC:

[cell.orangeText1 setText:@"Mikes Bar"];

<强>更新
根据您的评论,这是一个更新 您需要确保在视图出现时设置标签文本。不是在它初始化时。尝试使用viewWillAppearcellWillDisplay方法。

答案 2 :(得分:0)

执行此操作的标准方法是将UILabel作为单元格的属性公开。我强烈推荐这种方法。

或者,您可以为whiteText1和orangeText1实现自定义setter,然后在相应的标签上设置text属性。看起来你在initWithStyle中保留了对lblview的引用,所以你也需要这样做,以便你可以参考它。 (或者为标签指定标签,以便您可以使用标签从内容视图中检索标签。)