具有动态单元格高度的UITableview中的AutoLayout [英] AutoLayout in UITableview with dynamic cell height
问题描述
对于我的表格视图单元格的动态高度,我从此链接获取参考。
使用自动布局用于动态单元布局的UITableView&可变行高
这是我的tableview数据源代码和委托方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
返回arrTemp。计数;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = @AutoLAyoutCell ;
AutoLayoutTableViewCell * cell =(AutoLayoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
for([NSBundle mainBundle] loadNibNamed中的id currentObject:@AutoLayoutTableViewCellowner:self options:nil]){
if([currentObject isKindOfClass :[UITableViewCell类]]){
cell =(AutoLayoutTableViewCell *)currentObject;
休息;
}
}
}
cell.IBlblLineNo.text = [NSString stringWithFormat:@Line:%i,indexPath.row];
cell.IBlblLineText.text = [arrTemp objectAtIndex:indexPath.row];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280,1000)lineBreakMode:NSLineBreakByTruncatingTail];
cell.IBlblLineText.numberOfLines = expectedlineLabelSize.height / 17;
CGRect frmlbl = cell.IBlblLineText.frame;
frmlbl.size.height = expectedlineLabelSize.height;
cell.IBlblLineText.frame = frmlbl;
返回单元格;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutoLayoutTableViewCell * cell =(AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath];
cell.IBlblLineNo.text = [NSString stringWithFormat:@Line:%i,indexPath.row];
cell.IBlblLineText.text = [arrTemp objectAtIndex:indexPath.row];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGSize expectedlineLabelSize = [cell.lineLabel.text sizeWithFont:cell.lineLabel.font constrainedToSize:CGSizeMake(280,1000)lineBreakMode:NSLineBreakByWordWrapping];
cell.IBlblLineText.numberOfLines = expectedlineLabelSize.height / 17;
CGRect frmlbl = cell.IBlblLineText.frame;
frmlbl.size.height = expectedlineLabelSize.height;
cell.IBlblLineText.frame = frmlbl;
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] .height;
height + = 1.0f;
返回高度;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutoLayoutTableViewCell * cell =(AutoLayoutTableViewCell *)[IBtblAutoLayoutExample的cellForRowAtIndexPath:indexPath];
CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280,1000)lineBreakMode:NSLineBreakByTruncatingTail];
返回expectedlineLabelSize.height;
}
我有2个问题:
-
我的问题是我在行附近收到错误
EXE_BAD_EXCESS
/ p>
AutoLayoutTableViewCell * cell =(AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath];在$ code> heightForRowAtIndexPath
和estimatedHeightForRowAtIndexPath
。 -
为什么我必须在
cellForRowAtIndexPath $ c $中写标签文本c>和
heightForRowAtIndexPath
?
另外,我错过了为了获得单元的动态高度需要什么?
设置行高的自动尺寸&估计行高,确保执行以下步骤,自动尺寸对单元格/行高度布局有效。
- 分配和实现tableview dataSource和委托
- 分配
UITableViewAutomaticDimension
到rowHeight& estimatedRowHeight - 实现委托/ dataSource方法(即
heightForRowAt
并返回值UITableViewAutomaticDimension
到它)
-
目标C:
// ViewController.h中的$ code> //
#import< UIKit / UIKit.h>
@interface ViewController:UIViewController< UITableViewDelegate,UITableViewDataSource>
@property IBOutlet UITableView * table;
@end
//在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 {
返回UITableViewAutomaticDimension;
}
Swift:
@IBOutlet弱变量表:UITableView!
覆盖func viewDidLoad(){
super.viewDidLoad()
//不要忘记为表
表设置dataSource和delegate。 dataSource = self
table.delegate = self
//设置行高的自动尺寸
// Swift 4.2以后
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1及以下
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension计算标签内容/文本的高度
func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath) - > CGFloat {
// Swift 4.2以上
返回UITableView.automaticDimension
// Swift 4.1及以下
返回UITableViewAutomaticDimension
}
对于UITableviewCell中的标签实例
- 设置行数= 0(&换行模式=截断尾部)
- 相对于其superview / cell容器设置所有约束(顶部,底部,右侧)。
- 可选:如果您希望标签覆盖最小垂直区域,即使没有数据,也要设置标签的最小高度。
注意:如果您有多个动态长度的标签(UIElements),应根据其内容大小进行调整:调整内容拥抱和压缩阻力优先级`对于要以更高优先级扩展/压缩的标签。
For dynamic height of my table view cell I take reference from this link. Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
Here is my code of tableview data source and delegate methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return arrTemp. count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"AutoLAyoutCell";
AutoLayoutTableViewCell *cell=(AutoLayoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
for (id currentObject in [[NSBundle mainBundle] loadNibNamed:@"AutoLayoutTableViewCell" owner:self options:nil]) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (AutoLayoutTableViewCell *)currentObject;
break;
}
}
}
cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row];
cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail];
cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17;
CGRect frmlbl=cell.IBlblLineText.frame;
frmlbl.size.height=expectedlineLabelSize.height;
cell.IBlblLineText.frame=frmlbl;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath];
cell.IBlblLineNo.text=[NSString stringWithFormat:@"Line:%i",indexPath.row];
cell.IBlblLineText.text=[arrTemp objectAtIndex:indexPath.row];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
CGSize expectedlineLabelSize = [cell.lineLabel.text sizeWithFont:cell.lineLabel.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByWordWrapping];
cell.IBlblLineText.numberOfLines=expectedlineLabelSize.height/17;
CGRect frmlbl=cell.IBlblLineText.frame;
frmlbl.size.height=expectedlineLabelSize.height;
cell.IBlblLineText.frame=frmlbl;
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
height += 1.0f;
return height;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath];
CGSize expectedlineLabelSize = [cell.IBlblLineText.text sizeWithFont:cell.IBlblLineText.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:NSLineBreakByTruncatingTail];
return expectedlineLabelSize.height;
}
I have 2 questions :
My issue is I get the error
EXE_BAD_EXCESS
near the lineAutoLayoutTableViewCell *cell = (AutoLayoutTableViewCell *)[IBtblAutoLayoutExample cellForRowAtIndexPath:indexPath];
in
heightForRowAtIndexPath
andestimatedHeightForRowAtIndexPath
.Why do I have to write label text in both
cellForRowAtIndexPath
andheightForRowAtIndexPath
?
Also, am I missing anything needed to achieve dynamic height for the cell?
To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout.
- Assign and implement tableview dataSource and delegate
- Assign
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeight - Implement delegate/dataSource methods (i.e.
heightForRowAt
and return a valueUITableViewAutomaticDimension
to it)
-
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
}
For label instance in UITableviewCell
- Set number of lines = 0 (& line break mode = truncate tail)
- Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
- Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.
Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.
这篇关于具有动态单元格高度的UITableview中的AutoLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!