【发布时间】:2015-02-16 19:50:45
【问题描述】:
我正在使用 Xcode 6 并创建了一个新项目并在 iOS7 中遇到自动布局问题,因为它在 iOS8 中正常工作。 我正在使用以下代码:-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *lbl1=(UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = @"test fkdjsjk kjhsdf ksjh ksjdhf jks kjh kjdhf kjsd h kjh j ,jhfjdfshfk jh fkjsdh fkjs dhkfjhs dkjfh dksjhf jksd hfjkdhs fk jksdhf kjsdh fkjsdh fjkhdsk fjkdsh fkjdsh fkjdsh fkjs djkf djksf kjdshfkjds fkjdsh fkjhs dkjfh kdjs fkdjshfkdsjfh kdjsh fk";
[lbl1 setPreferredMaxLayoutWidth:lbl1.frame.size.width];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
cell.frame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(cell.bounds), CGRectGetHeight(tableView.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
return cell.frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *lbl1=(UILabel*)[cell.contentView viewWithTag:1];
lbl1.text = @"test fkdjsjk kjhsdf ksjh ksjdhf jks kjh kjdhf kjsd h kjh j ,jhfjdfshfk jh fkjsdh fkjs dhkfjhs dkjfh dksjhf jksd hfjkdhs fk jksdhf kjsdh fkjsdh fjkhdsk fjkdsh fkjdsh fkjdsh fkjs djkf djksf kjdshfkjds fkjdsh fkjhs dkjfh kdjs fkdjshfkdsjfh kdjsh fk";
[lbl1 setBackgroundColor:[UIColor greenColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
我的要求只是标签应该动态地采用高度作为 iOS 7 中提供给它的内容。
【问题讨论】:
-
你设置
lbl1.numberofline = 0了吗? -
这是为增长标签设置零行数的基本思路。
-
@Rahulgupta:检查我的答案
-
@Rahulgupta 你的问题是标签高度在增加但没有增加对吗?
-
你能把你得到的截图分享给我吗?因为通过将标签线分配给 0 本身会动态改变标签高度。您是在寻找标签高度还是单元格高度。
标签: ios iphone uitableview ios7 autolayout