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;
}
当您滚动 tableView
时,您可以看到第一个和第二个单元格中的标签现在显示良好。 (这里我只是滚动出两个单元格并将它们拉回来。)
最佳答案
在您的帖子中,我没有找到您用来设置标签框架的 labelBible 的定义。
使用约束很快:
和
通过这种方式,您不必在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中执行任何操作。
编辑:要使用约束:必须选中Use Autolayout
。
希望这会如您所愿。
关于ios - 无法在 tableView 中设置标签框 :cellForRowAtIndexPath: method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21909522/