ios - 无法在 tableView 中设置标签框 :cellForRowAtIndexPath: method

标签 ios uitableview ios7 uilabel

tableViewCell 中标签的边框不能在ableView:cellForRowAtIndexPath: 方法中设置。事实上,如 NSLog 中所示,标签的框架已更改但未显示在 View 中。 我希望标签(绿色背景)应该比图片中的高,这样可以显示其中的所有文字。在 Storyboard 中创建带有 Identifier“cell”的 Prototype Cell。任何帮助将不胜感激。

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;
}

enter image description here enter image description here

当您滚动 tableView 时,您可以看到第一个和第二个单元格中的标签现在显示良好。 (这里我只是滚动出两个单元格并将它们拉回来。)

after scroll the tableView

最佳答案

在您的帖子中,我没有找到您用来设置标签框架的 labelBible 的定义。

使用约束很快:

enter image description here

enter image description here

通过这种方式,您不必在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中执行任何操作。

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

enter image description here

希望这会如您所愿。

关于ios - 无法在 tableView 中设置标签框 :cellForRowAtIndexPath: method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21909522/

相关文章:

ios - 适配 Mapkit 应用

ios - 3D触摸: unexpectedly found nil while unwrapping an Optional value

ios - 在 iphone sdk 的屏幕中移动对象

xcode - iOS7 隐藏状态栏但不调整顶部布局指南

javascript - 使用 sencha 应用程序查看 Iphone 中的连接状态

ios - 如何在 ios 中的 uitextfield 的左 View 上添加左侧边距?

iphone - UITableViewCell 右边有图片?

ios - 自动布局 - 当一个 View 具有动态高度时,将两个 View 在 UITableViewCell 中垂直居中

ios - 如何在 moveRowAt : sourceIndexPath:? 后删除空白部分

ios - UINavigationBar 后退按钮在某些设备或模拟器上显示 "Back"标题,在其他设备或模拟器上显示之前的 View Controller 标题