UITableViewCell auto-sizing 高度不工作
UITableViewCell auto-sizing height not working
我的 table 视图中的单元格 auto-sizing 没有包含内容的高度。我正在为单元格原型使用 "Basic" 样式。我创建了一个仅包含我的视图控制器和故事板的新测试项目,它也有同样的问题。我做错了什么?
行高测试控制器:
@implementation RowHeightTestController
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Test" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = @"Testing";
cell.textLabel.font = [UIFont systemFontOfSize:60 + indexPath.row];
return cell;
}
@end
故事板:
我所看到的:
尝试设置table的属性:
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
如果内容更改,则使用通知重新加载 table
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
// This method is called when the Dynamic Type user setting changes (from the system Settings app)
- (void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
更多内容请观看这个精彩的回答:
为 tableView 创建 outlet/reference 并在 viewDidLoad 上添加
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
为此,您必须更新 tableView 的委托中的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
不是 willDisplay
或任何其他代表。
此外,您还必须在 storyboard/xib 中正确设置自动布局约束。
对于屏幕截图中所示的简单单元格视图,
为 UILabel 设置以下约束,如下所示:
- 领导 space 到 superview。
- 尾随 space 到 superview
- 置顶space到superview
- 底部 space 到超级视图
我的 table 视图中的单元格 auto-sizing 没有包含内容的高度。我正在为单元格原型使用 "Basic" 样式。我创建了一个仅包含我的视图控制器和故事板的新测试项目,它也有同样的问题。我做错了什么?
行高测试控制器:
@implementation RowHeightTestController
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Test" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = @"Testing";
cell.textLabel.font = [UIFont systemFontOfSize:60 + indexPath.row];
return cell;
}
@end
故事板:
我所看到的:
尝试设置table的属性:
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
如果内容更改,则使用通知重新加载 table
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
// This method is called when the Dynamic Type user setting changes (from the system Settings app)
- (void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
更多内容请观看这个精彩的回答:
为 tableView 创建 outlet/reference 并在 viewDidLoad 上添加
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
为此,您必须更新 tableView 的委托中的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
不是 willDisplay
或任何其他代表。
此外,您还必须在 storyboard/xib 中正确设置自动布局约束。
对于屏幕截图中所示的简单单元格视图, 为 UILabel 设置以下约束,如下所示:
- 领导 space 到 superview。
- 尾随 space 到 superview
- 置顶space到superview
- 底部 space 到超级视图