【发布时间】:2015-07-16 08:42:16
【问题描述】:
我使用自动布局创建了一个自定义表格视图单元格,如下图所示
应用启动后,一切正常。但是当我滚动 tableview 或选择一个视图时,单元格的布局将会改变。 发生了什么?
代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
[cell.textLabel setText: textToSHow];
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
[cell setNeedsLayout];
[cell layoutIfNeeded];
CGSize fitSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return fitSize.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
[cell.textLabel setText:textToSHow];
return cell;
}
运行应用程序:
当一个单元格被选中时:
非常感谢!
【问题讨论】:
-
当一切都设置为自动布局时,你为什么要计算行的
height? -
表格视图有一个名为:heightforrow 的数据源,所以我需要自己计算行高。
-
这不是必需的方法...您可以使用该方法或自动布局...如果您想使用代码,我认为您可以删除约束。
-
我不认为你可以通过这种方式成功,你能提供可以正确运行的完整源代码吗?
标签: ios objective-c uitableview autolayout