我正在使用此代码来调整用于维护我的单元格标题的标签的大小。问题是在我滚动表格之前,单元格标签不会调整大小。单元格需要消失才能正确调整其标签的大小。我可以做些什么来调整所有单元格标签的大小而无需事先滚动?
感谢阅读。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ExampleTableViewCell *cell = (ExampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExampleTableViewCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.title.text = [self.test objectAtIndex:[indexPath row]];
//Calculates the expected size based on the font and linebreak mode of the label.
CGSize maximumLabelSize = CGSizeMake(200, [UIScreen mainScreen].bounds.size.width);
CGSize expectedLabelSize = [cell.title.text sizeWithFont:cell.title.font constrainedToSize:maximumLabelSize lineBreakMode:cell.title.lineBreakMode];
// Adjusts the label the the new height.
CGRect newFrame = cell.title.frame;
newFrame.size.height = expectedLabelSize.height;
cell.title.frame = newFrame;
return cell;
}
最佳答案
尝试在
中编写相同的代码willDisplayCell
UItableViewController 的方法
另一种方法是......在你的 ExampleTableViewCell 类中实现
- (void)layoutSubviews{
}
在上面的函数中设置标题的框架。它应该工作
关于ios - 单元格标签在表格滚动之前不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785873/