无法在tableView:cellForRowAtIndexPath:方法中设置标签框架 [英] Failed to set label frame in tableView:cellForRowAtIndexPath: method

查看:132
本文介绍了无法在tableView:cellForRowAtIndexPath:方法中设置标签框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tableViewCell 中标签的框架无法在 ableView:cellForRowAtIndexPath:方法中设置。事实上,如 NSLog 中所示,标签的框架已更改,但未在视图中显示。 我预计标签(带绿色背景)应该高于图片中的标签,可以显示其中的所有文字原型单元格标识符单元格在故事板中创建。任何帮助将不胜感激。



ps:Xcode 5.0.2,iOS7

   - (void)viewDidLoad 
{
[super viewDidLoad];
NSString * str = @加载视图后进行任何其他设置,通常是从nib加载视图后进行任何其他设置,通常是从nib加载视图后进行任何其他设置,通常是从nib ;
NSArray * array = [NSArray arrayWithObjects:str,str,str,str,str,str,nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = indexPath.row;
CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width,2000);
CGSize size = [[array objectAtIndex:row] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@ {NSFontAttributeName:[UIFont fontWithName:FontName size:FontSize]} context:nil] .size;

返回size.height + 20;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = indexPath.row;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@cell];
UILabel * label =(UILabel *)[cell.contentView viewWithTag:1];
label.autoresizingMask = UIViewAutoresizingNone;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.font = [UIFont fontWithName:FontName size:FontSize];
label.backgroundColor = [UIColor greenColor];
label.text = [array objectAtIndex:row];

CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width,2000);
CGSize size = [label.text boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@ {NSFontAttributeName:label.font} context:nil] .size;
CGRect rect = label.frame;
rect.size.height = size.height;
[label setFrame:rect];
NSLog(@标签高度:%f,label.frame.size.height);
[label sizeThatFits:sizeConstraint];
返回单元格;
}


tableView 时,您可以看到第一个和第二个单元格中的标签现在已经很好地显示了p>

。 (这里我只滚动了两个单元格并将它们拉回来。)



后,alt =

解决方案

在你的帖子中我找不到你使用的labelBible的定义设置标签的框架。



使用约束它很快:









In这样你就不必在 - (UITableViewCell *)tableView中执行任何操作:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法。



编辑:使用约束:必须选中使用Autolayout





希望w你想要的工作不好。


The frame of the label in tableViewCell can not be set in the ableView:cellForRowAtIndexPath: method. In fact as show in the NSLog, the label's frame has changed but it did not show on the view. I expected the label(with green background) should be taller than in the picture, which can show all the text in it. The Prototype Cell with Identifier "cell" is created in the storyboard. Any help will be appreciated.

ps: Xcode 5.0.2, iOS7

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString * str = @"Do any additional setup after loading the view, typically from a nib Do any additional setup after loading the view, typically from a nib Do any additional setup after loading the view, typically from a nib";
    NSArray *array = [NSArray arrayWithObjects:str, str, str , str, str, str, nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width, 2000);
    CGSize size = [[array objectAtIndex:row] boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:FontName size:FontSize]} context:nil].size;

    return size.height + 20;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
    label.autoresizingMask = UIViewAutoresizingNone;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = [UIFont fontWithName:FontName size:FontSize];
    label.backgroundColor = [UIColor greenColor];
    label.text = [array objectAtIndex:row];

    CGSize sizeConstraint = CGSizeMake(tableView.frame.size.width, 2000);
    CGSize size = [label.text boundingRectWithSize:sizeConstraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: label.font} context:nil].size;
    CGRect rect = label.frame;
    rect.size.height = size.height;
    [label setFrame:rect];
    NSLog(@"label height :%f", label.frame.size.height);
    [label sizeThatFits:sizeConstraint];
    return cell;
}

when you scroll the tableView, you can see the label in the first and second cell are well displayed now. (Here i just scrolled out the two cells and pull them back.)

解决方案

In your post I don't find the definition of labelBible that you use to set the frame of the label.

Using constraint it's quick:

and

In this way you don't have to do anything in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

Edit: to use constraint: Use Autolayout has to be checked.

Hope that will work as you want.

这篇关于无法在tableView:cellForRowAtIndexPath:方法中设置标签框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆