Ios systemLayoutSizeFittingSize在外部Xib单元上不起作用
我已经为这个问题挣扎了两天 我有一个UIViewController,故事板上有一个UITableView 但是我想重用定制单元的设计,所以我使用Xib文件来设计单元 单元格包含一个标签,该标签在运行时获取内容,因此其高度会有所不同Ios systemLayoutSizeFittingSize在外部Xib单元上不起作用,ios,objective-c,uitableview,autolayout,xib,Ios,Objective C,Uitableview,Autolayout,Xib,我已经为这个问题挣扎了两天 我有一个UIViewController,故事板上有一个UITableView 但是我想重用定制单元的设计,所以我使用Xib文件来设计单元 单元格包含一个标签,该标签在运行时获取内容,因此其高度会有所不同 我正在使用autolayout,我有所有的约束条件,并将标签的线条设置为0 -(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view
我正在使用autolayout,我有所有的约束条件,并将标签的线条设置为0
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView registerNib:[UINib nibWithNibName:@"PostingTableViewCell" bundle: [NSBundle mainBundle]] forCellReuseIdentifier:@"PostingCell"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostingCell";
self.customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.customCell == nil)
{
self.customCell = [[PostingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *post = [self.posts objectAtIndex:indexPath.section];
NSString *contentString = [post objectForKey:@"content"];
self.customCell.contentLabel.text = contentString;
self.customCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(self.customCell.bounds));
[self.customCell setNeedsLayout];
[self.customCell layoutIfNeeded];
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
当我将自定义单元格放在情节提要中的tableViewController上时,我的代码可以工作,但当我将其迁移到xib文件时,tableviewcell的高度不再调整
这是我的xib文件和约束的屏幕截图
以下是我的cellForRowAtIndexPath方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"PostingCell";
PostingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *post = [self.posts objectAtIndex:indexPath.section];
NSString *contentString = [post objectForKey:@"content"];
cell.contentLabel.text = contentString;
}
模拟器截图
当我硬编码高度为150时,屏幕截图如下:
我还是不知道你为什么要在heightForRowAtIndexPath做这些 试试这个 1.调整xib中单元格的高度。 2.试试这样的
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.customCell.bounds.size.height;
}
您是否向自定义单元格添加了约束?我有所有约束只是好奇,为什么要在heightForRowAtIndexPath委托方法中设置单元格?另外,请发布一个它看起来像什么的截图。它在模拟器中看起来像什么?我想知道你想做什么我现在就截屏模拟器我的意思是它仍然给所有cellsTry硬编码返回150.0相同的高度我会为你发布截屏我仍然无法想出解决方案:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.customCell.bounds.size.height;
}