代码之家  ›  专栏  ›  技术社区  ›  pankaj

将uiLabel添加到自定义uiTableViewCell

  •  0
  • pankaj  · 技术社区  · 14 年前

    我正在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 回复  |  直到 14 年前
        1
  •  0
  •   Kris Markel    14 年前

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

    或者,可以为whitetext1和oragetext1实现自定义的setter,然后在适当的标签上设置text属性。它看起来不像您在initWithStyle中保留了对lblview的引用,所以您也需要这样做,以便可以引用回它。(或为标签指定一个标记,以便使用该标记从内容视图中检索该标签。)

        2
  •  0
  •   Gyani    14 年前

    将标签上的文本设置为

    cell.orangeText1.text = @"Mikes Bar";
    cell.orangeText1.text = @"4.3 mi northeast";
    cell.whiteText1.text = @"Level";
    cell.whiteText1.text = @"Freshman";
    
        3
  •  0
  •   mohdajami    14 年前

    正如吉亚尼所说,但你可以通过这样的写作使它更加客观:

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

    更新
    根据你的评论,这里有一个更新。
    当视图出现时,您需要确保设置标签文本完成。初始化时不会。尝试使用 viewWillAppear cellWillDisplay 方法。