【问题标题】:Adding Labels As subview to UIView将标签作为子视图添加到 UIView
【发布时间】:2012-08-22 11:38:45
【问题描述】:

谁能告诉我如何为我添加到 UITableView Cell 上的视图添加两个标签。我已经将该视图创建为 UIView 并带有一些名称。我在 UIView 类中创建了两个标签,并为标签设置了框架,设置文本等。我​​的问题是在 tableview 单元格中获取该视图,但不是那些标签。

    countLabel.text = @"4";
    countLabel.frame=CGRectMake(275, 10, 20, 15);
    countLabel.font=[UIFont boldSystemFontOfSize:15.0];
    countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
    countLabel.backgroundColor=[UIColor clearColor];

    hrsLabel.text = @"Hours";
    hrsLabel.frame=CGRectMake(260, 30, 45, 15);
    hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
    hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
    hrsLabel.backgroundColor=[UIColor clearColor];

这只是我正在设置框架,文本到 UIView.and 中的标签之类的标签

GreenView *greenView = [[GreenView alloc] initWithFrame:CGRectMake(250, 8, 60, 50)];
greenView.backgroundColor = [UIColor colorWithRed:0.000 green:0.690 blue:0.313 alpha:0.5];
[cell.contentView addSubview:greenView];

在这里,我将该 UIView 添加到 tableview 单元格。我不知道如何将这些标签添加到我的 UIView。请帮帮我。

如果英文有任何错误,请见谅。 任何人请帮助我。 非常感谢。

【问题讨论】:

  • 你能分享你将标签添加到 UIView 的代码吗?
  • 添加这些行,[self.contentView addSubview:countLabel]; [self.contentView addSubview: hrsLabel];
  • 此代码是否在单独的单元格类中?如果是这样,您需要分配标签。
  • 我刚刚编辑了我的疑问,请您看一下。
  • 我认为你这样做是错误的(如果我错了,请纠正我)。通常,如果您想将 UIView 添加到表格单元格,您所做的就是将标签添加到您在 UITableViewCell 子类中创建的特定视图中。

标签: iphone uiview uilabel


【解决方案1】:

创建 label 和 label1 等标签并添加到 UIView 中

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 250, 15)];

[label setText:@"Hello"];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];

[label1 setText:@"Hello1"];

UIView *myView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

[myView addSubview:label];
[myView addSubview:label1];

【讨论】:

  • 这里的SubView应该是我认为的Subview
  • 是的,谢谢 joshsverns
【解决方案2】:

像这样将标签添加到 GreeView

例如:

    GreenView *greenView = [[GreenView alloc] initWithFrame:CGRectMake(250, 8, 60, 50)];
    greenView.backgroundColor = [UIColor colorWithRed:0.000 green:0.690 blue:0.313 alpha:0.5];

    countLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 20, 15)];
    countLabel.text = @"4"; 
    countLabel.font=[UIFont boldSystemFontOfSize:15.0]; 
    countLabel.textColor=[UIColor whiteColor];
    countLabel.backgroundColor=[UIColor clearColor]; 
    [greenView addSubview:countLabel];

    hrsLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 45, 15)];
    hrsLabel.text = @"Hours";
    hrsLabel.font=[UIFont boldSystemFontOfSize:15.0]; 
    hrsLabel.textColor=[UIColor whiteColor]; 
    hrsLabel.backgroundColor=[UIColor clearColor];
    [greenView addSubview:hrsLabel];

    [cell.contentView addSubview:greenView];

希望这会对你有所帮助。

【讨论】:

  • 我认为这是为 tableviewCell 添加标签。我需要在 tableviewCell 中存在的视图上添加标签..
【解决方案3】:
countLabel.text = @"4";
countLabel.frame=CGRectMake(275, 10, 20, 15);
countLabel.font=[UIFont boldSystemFontOfSize:15.0];
countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
countLabel.backgroundColor=[UIColor clearColor];

hrsLabel.text = @"Hours";
hrsLabel.frame=CGRectMake(260, 30, 45, 15);
hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
hrsLabel.backgroundColor=[UIColor clearColor];

    [greenView  addSubview: countLabel];
    [greenView  addSubview: hrsLabel];
    [cell.contentview addSubview:greenView];

    return cell;

【讨论】:

  • 是的@Rupesh。我同意你的回答。但我用 UIView 的子类创建了作为 Objective-C 的视图。所以在 .m 文件中我没有得到 [myView addSubView:] 方法我明白了now.as 我只是添加喜欢 [self addSubview:countLbl] 并感谢您的回复
【解决方案4】:
countLabel.text = @"4";
countLabel.frame=CGRectMake(275, 10, 20, 15);
countLabel.font=[UIFont boldSystemFontOfSize:15.0];
countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
countLabel.backgroundColor=[UIColor clearColor];

hrsLabel.text = @"Hours";
hrsLabel.frame=CGRectMake(260, 30, 45, 15);
hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
hrsLabel.backgroundColor=[UIColor clearColor];

[self addSubView:countLabel];
[self addSubView:hrsLabel];

最后我得到了上面的答案。非常感谢您的所有回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    相关资源
    最近更新 更多