2012-03-12 64 views
0

我将标签添加到uiimage,然后将该图像添加到可用视图中。但它没有显示。任何人都可以帮忙吗?将uilabel添加到显示在适合视图上的uiimage上

这是代码我使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    } 
//image which contains label 
    UIImageView *BgImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-bg.png"]]; 
    BgImage.frame=cell.frame; 
//label which will be on image 
    UILabel *NameLAbel=[[UILabel alloc]initWithFrame:CGRectMake(25, 5, 45, 45)]; 


    NameLAbel.textColor=[UIColor blackColor]; 

    if (indexPath.row==0) { 
     [email protected]"Home"; 
    } 
    else if (indexPath.row==1) { 
     [email protected]"You"; 
    } 
    else if (indexPath.row==2) { 
     [email protected]"Contacts"; 
    } 
    else if (indexPath.row==3) { 
     [email protected]"Settings"; 
    } 
    else { 
     [email protected]"Sign Out"; 
    } 
    NSLog(@"%@",NameLAbel.text); 

    [BgImage addSubview:NameLAbel]; 
//adding image on cell 
    [cell.contentView addSubview:BgImage]; 


    [NameLAbel release]; 
    [BgImage release]; 
    return cell; 
} 
+1

只是标签没有显示吗?或没有显示? – 2012-03-12 14:51:24

回答

1

我不知道你是否能真正增加UILabelUIImageView,虽然他们都是UIView子类,我认为imageview的将只显示图像。

尝试在将图像添加到视图后将标签添加到cell.contentView。这应该会达到你以后的影响。

要么,或UIView创建将举行两个UIImageViewUILabel

+0

你可以添加UILabel到UIImageView,现在就试试它:) – janusbalatbat 2012-03-12 14:56:07

+0

好吧,我的坏。尽管将标签添加到contentView仍然可以实现OP的功能。除非我从他的代码中遗漏了一些东西。 – 2012-03-12 14:58:37

+1

@sunajledif:这可能是可能的,但UIImageView是UIView类之间的异常。例如,如果将其子类并将它的'needsDisplay'设置为YES,则不会像所期望的那样调用drawRect:。所以阿米特和博加特尔在这里有一个观点。它可以保存子视图 - 这也是事实。 – 2012-03-12 15:29:28

1

添加一些UI控件作为一个UIImageView的子视图听起来实在令人质疑。即使它现在有效,将来也可能不起作用,我相信这违反了UIKit约定。如果你想让一个视图看起来像是另一个视图的“一部分”,那么把它们放在另一个视图中,或者直接在cell.contentView中作为Amit注释,或者如果你想剪裁,使标签看起来像被裁剪通过图像,创建一个UIView容器,启用该视图的“剪辑子视图”设置,并将UIImageView和UILabel作为兄弟在该容器UIView中。