在运行应用程序时,在cellForRowAtIndexPath中设置UILabel高度实际上并没有改变我的标签高度

时间:2014-07-09 07:40:07

标签: ios objective-c uitableview uilabel

我试图在运行时动态设置我的UITableViewCell内部标签的高度。标签的高度取决于内容。我在我的自定义UITableViewCell xib文件中设置了UILabel应该有0行(没有边界)。下面是我的CellForRowAtIndexPath代码。我环顾四周,尝试了一些东西,但似乎没有什么对我有用。

XYZPlayerTableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    static NSString *CellIdentifier = @"PlayerCell";
    XYZPlayerTableViewCell *cell = (XYZPlayerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    XYZPlayer *player = [self.players objectAtIndex:row];
    NSString *nameText = player.name;
    NSString *gameText = player.game;

    cell.nameLabel.text = nameText;
    cell.gameLabel.text = gameText;
    [cell.gameLabel sizeToFit];
    cell.gameLabel.numberOfLines = 0;
    cell.ratingImageView.image = [self imageForRating:player.rating];

    CGSize boundingRect = CGSizeMake(cell.frame.size.width, 4000);
    CGRect expectedFrame = [cell.gameLabel.text boundingRectWithSize:boundingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: cell.gameLabel.font}  context:nil];
    cell.gameLabel.frame = expectedFrame;

    NSLog(@"gameLabel = 0x%@  gameText = %@  gameLabelHeight = %.2f", cell.gameLabel, gameText, expectedFrame.size.height);
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;

    //set width depending on device orientation
    self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x, self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);

    // Get variables
    XYZPlayer *player = [self.players objectAtIndex:row];
    UILabel *nameLabel = self.cellPrototype.nameLabel;
    NSString *nameText = player.name;
    UILabel *gameLabel = self.cellPrototype.gameLabel;
    NSString *gameText = player.game;

    // Calculate height
    CGFloat nameLabelHeight =  [self sizeOfLabel:nameLabel withText:nameText].height;
    CGFloat gameLabelHeight = [self sizeOfLabel:gameLabel withText:gameText].height;
    CGFloat padding = nameLabel.frame.origin.y;
    CGFloat combinedHeight = padding + nameLabelHeight + padding/2 + gameLabelHeight + padding;

    return combinedHeight;
}

XYZPlayerTableViewCell.m

-(void)setGameLabelHeightForString:(NSString *)string
{
    self.gameLabel.numberOfLines = 0;
    gameLabel.text = string;
    CGSize maximumLabelSize = CGSizeMake(gameLabel.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [string sizeWithFont:self.gameLabel.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 
    CGRect newFrame = self.gameLabel.frame;
    newFrame.size.height = expectedLabelSize.height;
    self.gameLabel.frame = newFrame;
}


-(void) layoutSubviews
{
    [self setGameLabelHeightForString:gameLabel.text];
}

运行时的输出是:

2014-07-09 00:21:32.277 MyTestApp [24503:60b]

gameLabel = 0x< UILabel:0x8e42800; frame =(0 0; 321.946 81.124); text ='Tic-Tac-Toe和另一个......'; opaque = NO; autoresize = RM + BM; userInteractionEnabled = NO; layer =< CALayer:0x8e360d0>>

gameText = Tic-Tac-Toe另一条线,也许还有一些,因为谁知道这可以用多长时间。我想我需要更多的文字来使这个超级明显

gameLabelHeight = 81.12

这就是我的应用程序的样子。灰色显示标签的高度 Screen shot

任何人都对我做错了什么有任何想法?

6 个答案:

答案 0 :(得分:0)

您可以使用它来计算身高,但这不是推荐的方式

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

     XYZPlayerTableViewCell *cell=(XYZPlayerTableViewCell *)[self tableView:tblView cellForRowAtIndexPath:indexPath];
     return CGRectGetMaxX(cell.gameLabel.frame)+5;
}

答案 1 :(得分:0)

再使用一行代码

cell.gameLabel.numberOfLines = 0;

答案 2 :(得分:0)

要根据您的内容调整标签大小,您必须确保2件事

行数= 0 并在您的标签上调用 sizeToFit

不确定您是否设置了线条。尝试仅在委托内设置行,否则看起来没问题

 UILabel *tempLabel = [[UILabel alloc]initWithFrmae:_textView.bounds];
 tempLabel.numberOfLines = 0; // Important to do 
 tempLabel.text = @"Your Text";
 [tempLabel sizeToFit]; // This will resize your Label and now you can use it's height to manage your textView.

这会根据它的文字调整tempLabel的大小。 尝试仅在委托内设置numberOfLines。 还要尝试根据您的标签给出单元格的高度,方法是在 heightForRowAtIndexPath 委托

中计算它们

答案 3 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    float calulatedWidth = [self getWidthFromString:textTobePutInLabel];
    float calculatedHeight = [self getHeightFromString:textTobePutInLabel];
    cell.gameLabel.frame = CGRectMake(yourX,yourY,calulatedWidth,calculatedHeight)
    cell.gameLabel.text = gameText;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   float height = [self getHeightFromString:textTobePutInLabel];
   return height;
}

- (float)getWidthFromString:(NSString *)strName{

    CGSize maximumSize = CGSizeMake(9999, 9999);

    CGSize size = [strName boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:MAVEN_PRO size:13.0f]} context:nil].size;

    return size.width;
}

- (float)getHeightFromString:(NSString *)strName{

    CGSize maximumSize = CGSizeMake(9999, 9999);

    CGSize size = [strName boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:MAVEN_PRO size:13.0f]} context:nil].size;

    return size.height;
}

答案 4 :(得分:0)

你需要做两件事

  1. 覆盖heightForRowAtIndexPath UITableViewDelegate以根据文字动态返回单元格的高度。

  2. 覆盖layoutSubviews UITableViewCell(XYZPlayerTableViewCell)重新计算并设置标签框架。

  3. 你可以使用

    CGSize labelSize = [self.gameLabel.text sizeWithFont:[UIFont systemFontOfSize:11]
                                                 constrainedToSize:CGSizeMake(300, NOTIFICATION_DESC_MAX_HEIGHT + 5)
                                                     lineBreakMode:NSLineBreakByWordWrapping];
    

    如果要将String设置为label或

    ,则计算最佳拟合高度
    CGRect rect = [self.gameLabel.attributedText boundingRectWithSize:CGSizeMake(300, NOTIFICATION_DESC_MAX_HEIGHT + 5)
                                                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                        context:nil];
    

    如果您使用AttributedString作为标签

    - 维沙尔

答案 5 :(得分:0)

我检查了你的代码,我注意到在设置标签的框架后放置了[gameLabel sizeToFit];语句的一个问题。

即,在这些行之后

CGSize boundingRect = CGSizeMake(cell.frame.size.width, 4000);
CGRect expectedFrame = [cell.gameLabel.text boundingRectWithSize:boundingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: cell.gameLabel.font}  context:nil];
cell.gameLabel.frame = expectedFrame;