Iphone iOS-tableview单元格内的UILabel

Iphone iOS-tableview单元格内的UILabel,iphone,ios,cocoa-touch,uitableview,Iphone,Ios,Cocoa Touch,Uitableview,我对iphone的开发非常陌生,在某一点上我感到很疲惫。实际上,我需要在UITableView中显示一些结果,然后显示UITableView的单元格的UILables。。 如何使ui标签的根据文本调整内容。实际上,文本不是静态的。它可能在运行时更改。因此我需要将动态大小设置为UILabel。其次,假设文本为null,则它不应显示任何空格,并且应立即启动下一个UILabel。我如何才能做到这一点? 这是我的密码 self.tableView=[[UITableView alloc] initWit

我对iphone的开发非常陌生,在某一点上我感到很疲惫。实际上,我需要在
UITableView
中显示一些结果,然后显示
UITableView的
单元格的
UILables
。。 如何使
ui标签的
根据文本调整内容。实际上,文本不是静态的。它可能在运行时更改。因此我需要将动态大小设置为UILabel。其次,假设文本为
null
,则它不应显示任何空格,并且应立即启动下一个
UILabel
。我如何才能做到这一点? 这是我的密码

self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
            self.tableView.delegate=self;
            self.tableView.dataSource=self;
            self.tableView.scrollEnabled = NO;
            [self.view addSubview:self.tableView];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(currentHtmlElement==@"3")
    {
        static NSString *CellIdentifier=@"Cell";

        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];
        }
        NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
        cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
       UILabel  *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
        name1.font=[UIFont boldSystemFontOfSize:16];
        [name1 setTextAlignment:UITextAlignmentLeft];
        [name1 setText:[d valueForKey:@"Name"]];
        name1.tag = 111;
        [cell addSubview:name1];
        [name1 release];

        UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
        codetype .font=[UIFont boldSystemFontOfSize:12];        
        [codetype setTextAlignment:UITextAlignmentLeft];
        [codetype  setText:[d valueForKey:@"Code"]];
        codetype.tag=112;
        [cell addSubview:codetype];
        [codetype  release];    

        UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
        id .font=[UIFont fontWithName:@"arial" size:12];        
        [id setTextAlignment:UITextAlignmentLeft];
        [id  setText:[d valueForKey:@"ID"]];
        id.tag=113;
        [cell id ];
        [id  release]; 

        UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
        address .font=[UIFont fontWithName:@"arial" size:12];        
        [address setTextAlignment:UITextAlignmentLeft];
        [address  setText:[d valueForKey:@"address"]];
        line2.tag=114;
        [cell addSubview: address];
        [address  release]; 

        UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
        city .font=[UIFont fontWithName:@"arial" size:12];        
        [city  setTextAlignment:UITextAlignmentLeft];
        [city  setText:[d valueForKey:@"City"]];
        city.tag=115;
        [cell addSubview:city ];
        [city  release];
}

在这里,我已经将所有UILabel设置为静态大小。但我需要的是动态大小。假设地址标签为空,则它不应显示任何空间,并且下一个标签(即城市)应在id之后立即开始。这怎么可能呢

您可以使用以下代码使
可编辑
调整内容

CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font]; 
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
这里设置了第二个标签,如下图所示

更新

     if([[d valueForKey:@"address"] isEquelToString:@""]){
          UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
          city .font=[UIFont fontWithName:@"arial" size:12];        
          [city  setTextAlignment:UITextAlignmentLeft];
          [city  setText:[d valueForKey:@"City"]];
          city.tag=115;
          [cell addSubview:city ];
          CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font]; 
          city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }
      else{
          UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:@"arial" size:12];        
          [address setTextAlignment:UITextAlignmentLeft];
          [address  setText:[d valueForKey:@"address"]];
          line2.tag=114;
          [cell addSubview: address];
          CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font]; 
          address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }
每种标签都是如此

对于整个标签,请遵循此逻辑


我希望这对您有所帮助…

问题是您必须使用高度来绘制索引路径。问题是它是在cellForRowAtIndexPath之前调用的,因此文本不在组件中,您不能简单地根据文本调整组件大小。因此,您要做的是使用sizeWithFont计算文本大小

正常/标准文本的情况下

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSString* theText =  ... ; 

    //then calculate the size. systemFontOfSize depends on your font size
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSAttributedString* theText =  ...;

    //then calculate the size.
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];

return rectSize.size.height;

    return textSize.height;
}
属性文本的情况下

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSString* theText =  ... ; 

    //then calculate the size. systemFontOfSize depends on your font size
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSAttributedString* theText =  ...;

    //then calculate the size.
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];

return rectSize.size.height;

    return textSize.height;
}

我在我的代码中完成了这个任务。。。。您可以使用此代码并根据您的代码进行更改…如果有任何问题,请告诉我

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //code to get notification text height
    NSString *text = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 100.0f);
    CGSize size = [text sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    return size.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        //label for message
        UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
        lblMsg.tag = 12;
        lblMsg.backgroundColor = [UIColor clearColor];
        [lblMsg setFont:[UIFont fontWithName:@"Verdana" size:12]];
        [lblMsg setLineBreakMode:UILineBreakModeWordWrap];

        [cell addSubview:lblMsg];
    }

    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
    NSString *txt = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 2000.0f);
    CGSize size = [txt sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    //set frame height to labelMsg
    UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
    lblMsg.frame = CGRectMake(15,25, 175, size.height);
    [lblMsg setNumberOfLines:size.height/16];

    //set labelMsg text
    lblMsg.text = [dicNotification  objectForKey:@"message"];

    return cell;
}


根据我的说法…您的文本在标签中重叠..为什么不使用可重复使用的单元格..?我实际需要的是第一个标签,我将给出固定的尺寸。从第二个标签开始,标签应根据prevoius标签的内容自动设置其尺寸(y轴和高度)。如果假设,若一个标签的值为空,那个么该标签应该消失,下一个标签应该立即开始..我怎么做?这与Xcode无关。你们应该只在StackOverflown上使用英语。实际上,我需要的是,若地址为空,那个么它在id之后显示空白,然后下一个城市值开始。但我需要的是它如果地址为空,则不应显示任何空白,城市应立即开始…我如何做?我实际需要的是第一个标签,我将给出固定的尺寸。从第二个标签开始,标签应根据prevoius标签的内容自动设置其尺寸(y轴和高度)。如果假设,如果一个标签的值为空,则该标签应消失,下一个标签应立即开始..我如何做?@Esha在此处查看更新的答案我只发布了一个标签的代码,因此将此条件用于另一个标签亲爱的,我只给出此解决方案的想法,希望您理解...)但是在代码中,u为UILabel设置了一些静态高度。我需要根据内容动态设置其高度。如果假设我在一个标签中有一些大数据,那么它不会显示整个数据,并且会出现下一个标签。因此,我需要标签的动态高度……我该怎么做?我实际需要的是为第一个标签设置固定高度尺寸。从第二个标签开始,标签应根据上一个标签的内容自动设置其尺寸(y轴和高度)。如果假设,如果一个标签的值为空,则该标签应消失,下一个标签应立即开始。我该怎么做?@Esha,如我所示,您必须动态计算所有尺寸。我给出的过程是一个细胞。必须循环浏览标签列表,定义每个标签高度。请注意,HeightForRowatinex返回的高度是单元格的高度,该高度必须与所有标签(+填充)的总和相匹配。我假设您使用的是故事板,您应该使用所有标签创建一个自定义单元格,并调整它们的大小,然后将它们放置在heightForRowAtIndexPath中。@Esha此链接应该有助于您理解我的意思。然后进入“设计我们自己的原型细胞”。就我个人而言,我不使用标签。但所呈现的是想法。您已经创建了自定义单元格。您不需要在运行时创建标签,只需在运行时调整标签的大小和位置,这是在HeightForRowatinex中完成的。@Esha这样做也将允许您“重用”通过简单地放置标签和调整大小标签,而不重新创建它们,这将对性能有很大帮助。我通过编程创建了tableView。你能告诉我如何创建吗?我应该从哪里开始吗?对不起,我没有清楚地理解你的逻辑。如果heightForRowAtIndexPath中有很多UILabel,我应该为所有UILabel编写它吗?有什么具体的原因吗尺寸。高度/16?你能解释一下我一开始有点忘了排的高度吗…集中精力做标签。。。首先,我创建了字体大小为12的标签。。。然后在“txt”中有一些文本。。我的标签宽度为175,因此我将约束设置为175。现在在得到尺寸后,我正在设置标签高度。。。现在设置numberoflines=100;这取决于您,使用size.height/16我得到的是标签中需要多少行,。。我知道你在想为什么不使用size.height/12->那么原因是->文本在标签之间..所以上面填充2个像素,下面填充2个像素。。12+4 =16. 如果你实现它,你会得到我的逻辑:)我在一个单元格中有很多标签。在你的代码中,如果我不知道标签的行数,那么我怎么知道它下面另一个标签的y原点?我的意思是第二个标签应该从哪里开始?好的,你能告诉我abt返回大小。高度+35;在heightForRowAtIndexPath中?是否根据其中的内容调整行高?