动态增加 UILabel 和 TableView Cell 的高度?
Dynamically increase height of UILabel & TableView Cell?
我有一个 UITableView,我在其中显示自定义 cell.I 我的单元格有两个标签和一个视图,如下图所示。
我这样给了左视图约束
项目标签限制
中心视图约束
右视图约束
我正在使用一个 bean class 来存储两个标签的数据并将该 bean 对象添加到一个标签中 array.I 我在 heightForRowAtIndexPath 中使用以下代码。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Calculate a height based on a cell
if(!self.customCell) {
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"thirdcell"];
}
// Configure the cell
Instrument *inst=[arr_instSpecs objectAtIndex:indexPath.row];
self.customCell.label_item.text=inst.item;
self.customCell.label_desc.text=inst.desc;
// Layout the cell
[self.customCell layoutIfNeeded];
// Get the height for the cell
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// Padding of 1 point (cell separator)
CGFloat separatorHeight = 1;
return height + separatorHeight;
}
问题既不是标签的高度在增加,也不是 table 视图的高度 cell.I 已经解释了一切。我想在标签文本增加时增加标签的大小,并且当标签大小增加时单元格的高度必须增加。
将标签约束设置为 Cell 的顶部,将底部设置为单元格,并保持 Leading 和 Trailing 约束不变。
在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
通过使用 UITableViewAutomaticDimension 将根据您的标签内容增加您的 tableview 单元格高度,最重要的一点是,select 您的标签然后转到 Identity Inspector 并在行数中写入 0
您需要在加载视图时定义最大估计高度
tblObject.estimatedRowHeight = 300;
tblObject.rowHeight = UITableViewAutomaticDimension;
首先你不应该在自动布局环境中手动计算高度。只需将标签 TopSpace 和 BottomSpace 设置为单元格的 contentView,并确保将标签 NumberOfLines 设置为 0 并将 LineBreakMode 设置为 WordWrap。
其他约束如下,
物品标签:
分隔视图:
描述标签:
并如下添加高度代表,
#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
你应该得到如下输出,
希望对您有所帮助。
我就是这样锻炼的。
1) 设置编号行数为 0.
2) 设置换行。
3) 添加约束。
4) 更改垂直内容拥抱优先级。
5) 在 ViewDidLoad 上:
override func viewDidLoad() {
super.viewDidLoad()
self.sampleTableView.estimatedRowHeight = 600.0;
self.sampleTableView.rowHeight = UITableViewAutomaticDimension;
self.sampleTableView.setNeedsLayout()
self.sampleTableView.layoutIfNeeded()
}
6) 在 TableView 自定义单元格上:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
sizeToFit()
layoutIfNeeded()
}
要为行高和估计行高设置自动尺寸,请确保按照以下步骤制作,自动尺寸对 cell/row 高度布局有效。
- 分配并实现表视图数据源和委托
- 将
UITableViewAutomaticDimension
分配给 rowHeight & estimatedRowHeight
- 实施delegate/dataSource方法(即
heightForRowAt
和return一个值UITableViewAutomaticDimension
)
-
Objective C:
// in ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property IBOutlet UITableView * table;
@end
// in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
self.table.rowHeight = UITableViewAutomaticDimension;
self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Swift:
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
对于 UITableviewCell 中的标签实例
- 设置行数=0(&换行模式=截尾)
- 设置关于其父视图/单元格容器的所有约束(顶部、底部、左右)。
- 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。
注意:如果你有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:调整'Content Hugging and Compression Resistance Priority ` 对于您想要 expand/compress 具有更高优先级的标签。
Swift 3 / Swift 4
自定义单元格:
class CustomCell: UITableViewCell {
@IBOutlet var messageLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
sizeToFit()
layoutIfNeeded()
}
}
viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
和 cellForRowAtIndexPath:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
cell.messageLabel.text = exampleContent[indexPath.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.layoutIfNeeded()
return cell
}
Swift 4.2
对于 Basic
或 Subtitle
类型的单元格,首先在 viewDidLoad
中执行此操作:
self.tableView.estimatedRowHeight = 44 // This is the default cell height
self.tableView.rowHeight = UITableView.automaticDimension
然后select标题并将其行数设置为0。现在该行将根据标题文本调整其高度。您无需执行任何其他操作。
您的自定义单元格类型也是如此。确保您在自定义单元格中使用的标签也设置为 0 行。
我正在处理其他人的代码,none 给定的答案对我有用。约束是正确的。
将标签的“所需宽度”更改为自动,在情节提要中为我完成了工作。为此,请取消选中标签尺寸检查器中的“显式”复选标记。
我有一个 UITableView,我在其中显示自定义 cell.I 我的单元格有两个标签和一个视图,如下图所示。
我这样给了左视图约束
项目标签限制
中心视图约束
右视图约束
我正在使用一个 bean class 来存储两个标签的数据并将该 bean 对象添加到一个标签中 array.I 我在 heightForRowAtIndexPath 中使用以下代码。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Calculate a height based on a cell
if(!self.customCell) {
self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"thirdcell"];
}
// Configure the cell
Instrument *inst=[arr_instSpecs objectAtIndex:indexPath.row];
self.customCell.label_item.text=inst.item;
self.customCell.label_desc.text=inst.desc;
// Layout the cell
[self.customCell layoutIfNeeded];
// Get the height for the cell
CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// Padding of 1 point (cell separator)
CGFloat separatorHeight = 1;
return height + separatorHeight;
}
问题既不是标签的高度在增加,也不是 table 视图的高度 cell.I 已经解释了一切。我想在标签文本增加时增加标签的大小,并且当标签大小增加时单元格的高度必须增加。
将标签约束设置为 Cell 的顶部,将底部设置为单元格,并保持 Leading 和 Trailing 约束不变。 在
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
通过使用 UITableViewAutomaticDimension 将根据您的标签内容增加您的 tableview 单元格高度,最重要的一点是,select 您的标签然后转到 Identity Inspector 并在行数中写入 0
您需要在加载视图时定义最大估计高度
tblObject.estimatedRowHeight = 300;
tblObject.rowHeight = UITableViewAutomaticDimension;
首先你不应该在自动布局环境中手动计算高度。只需将标签 TopSpace 和 BottomSpace 设置为单元格的 contentView,并确保将标签 NumberOfLines 设置为 0 并将 LineBreakMode 设置为 WordWrap。
其他约束如下,
物品标签:
分隔视图:
描述标签:
并如下添加高度代表,
#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
你应该得到如下输出,
希望对您有所帮助。
我就是这样锻炼的。
1) 设置编号行数为 0.
2) 设置换行。
3) 添加约束。
4) 更改垂直内容拥抱优先级。
5) 在 ViewDidLoad 上:
override func viewDidLoad() {
super.viewDidLoad()
self.sampleTableView.estimatedRowHeight = 600.0;
self.sampleTableView.rowHeight = UITableViewAutomaticDimension;
self.sampleTableView.setNeedsLayout()
self.sampleTableView.layoutIfNeeded()
}
6) 在 TableView 自定义单元格上:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
sizeToFit()
layoutIfNeeded()
}
要为行高和估计行高设置自动尺寸,请确保按照以下步骤制作,自动尺寸对 cell/row 高度布局有效。
- 分配并实现表视图数据源和委托
- 将
UITableViewAutomaticDimension
分配给 rowHeight & estimatedRowHeight - 实施delegate/dataSource方法(即
heightForRowAt
和return一个值UITableViewAutomaticDimension
)
-
Objective C:
// in ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property IBOutlet UITableView * table;
@end
// in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
self.table.rowHeight = UITableViewAutomaticDimension;
self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Swift:
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
对于 UITableviewCell 中的标签实例
- 设置行数=0(&换行模式=截尾)
- 设置关于其父视图/单元格容器的所有约束(顶部、底部、左右)。
- 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。
注意:如果你有多个具有动态长度的标签(UIElements),应根据其内容大小进行调整:调整'Content Hugging and Compression Resistance Priority ` 对于您想要 expand/compress 具有更高优先级的标签。
Swift 3 / Swift 4
自定义单元格:
class CustomCell: UITableViewCell {
@IBOutlet var messageLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
sizeToFit()
layoutIfNeeded()
}
}
viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
和 cellForRowAtIndexPath:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
cell.messageLabel.text = exampleContent[indexPath.row]
cell.messageLabel.sizeToFit()
cell.messageLabel.layoutIfNeeded()
return cell
}
Swift 4.2
对于 Basic
或 Subtitle
类型的单元格,首先在 viewDidLoad
中执行此操作:
self.tableView.estimatedRowHeight = 44 // This is the default cell height
self.tableView.rowHeight = UITableView.automaticDimension
然后select标题并将其行数设置为0。现在该行将根据标题文本调整其高度。您无需执行任何其他操作。
您的自定义单元格类型也是如此。确保您在自定义单元格中使用的标签也设置为 0 行。
我正在处理其他人的代码,none 给定的答案对我有用。约束是正确的。 将标签的“所需宽度”更改为自动,在情节提要中为我完成了工作。为此,请取消选中标签尺寸检查器中的“显式”复选标记。