Kru*_*nal 11 objective-c row-height uitableview ios swift
我在我的应用程序中使用CustomCell实现了TableView,
我想UITableViewCell
根据文字长度调整我的动态高度UITableViewCell
,
这是快照 Customcell
:这是我的快照
UITableView
:代码段
heightForRowAtIndexPath
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [DescArr objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 100.0);
return height;
}
Run Code Online (Sandbox Code Playgroud)
正如您在第二张图片中看到的那样,单元格的高度是固定的,它不会随着文本(内容)大小而变化.
我哪里弄错了?如何根据其内容/文本制作标签或单元格以更新其大小?
gbk*_*gbk 13
请看这里 - 动态表视图单元格高度和自动布局教程.
你需要什么:
方法:
//this will calculate required height for your cell
-(CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
static UIYourClassCellName *sizingCell = nil;
//create just once per programm launching
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:@"identifierOfCell"];
});
[self configureBasicCell:sizingCell atIndexPath:indexPath];
return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
//this method will calculate required height of cell
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}
Run Code Online (Sandbox Code Playgroud)
并致电
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self heightForBasicCellAtIndexPath:indexPath];
}
Run Code Online (Sandbox Code Playgroud)
细胞的配置
- (void)configureBasicCell:(RWBasicCell *)cell atIndexPath:(NSIndexPath *)indexPath {
//make some configuration for your cell
}
Run Code Online (Sandbox Code Playgroud)
在接下来的所有操作之后(单元格内的文本仅作为占位符):
小智 6
以下代码对我来说很好.试试这个
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat lRetval = 10;
CGSize maximumLabelSize = CGSizeMake(231, FLT_MAX);
CGSize expectedLabelSize;
CGFloat numberoflines = [thirdcellText length]/17.0;
if (indexPath.section == 0) {
expectedLabelSize = [firstcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
constrainedToSize:maximumLabelSize
lineBreakMode:NSLineBreakByWordWrapping];
lRetval = expectedLabelSize.height;
}
else if(indexPath.section == 1)
{
expectedLabelSize = [secondcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
constrainedToSize:maximumLabelSize
lineBreakMode:NSLineBreakByWordWrapping];
lRetval = expectedLabelSize.height;
}
else if (indexPath.section == 2)
{
expectedLabelSize = [thirdcellText sizeWithFont:[UIFont systemFontOfSize:16.0]
constrainedToSize:CGSizeMake(231, numberoflines*17.0)
lineBreakMode:NSLineBreakByWordWrapping];
lRetval = expectedLabelSize.height-128.0;
}
UIImage *factoryImage = [UIImage imageNamed:NSLocalizedString(@"barcode_factory_reset.png", @"")];
CGFloat height = factoryImage.size.height;
if (lRetval < height) {
lRetval = height+15.0;
}
return lRetval;
}
Run Code Online (Sandbox Code Playgroud)
尝试在customcell类autolayout方法中添加以下代码
textview.frame = frame;
CGRect frame1 = textview.frame;
frame1.size.height = textview.contentSize.height-2;
textview.frame = frame1;
textview.contentSize = CGSizeMake(textview.frame.size.width, textview.frame.size.height);
labelPtr.frame = CGRectMake(CGRectGetMinX(imageView.frame)+CGRectGetMaxX(imageView.frame)+5.0, textview.frame.size.height+10.0, 140, 16.0);
[labelPtr setNeedsDisplayInRect:labelPtr.frame];
Run Code Online (Sandbox Code Playgroud)
尝试设置如下的标签属性
labelPtr = [[UILabel alloc] initWithFrame:CGRectZero];
labelPtr.backgroundColor =[UIColor clearColor];
[labelPtr setNeedsLayout];
[labelPtr setNeedsDisplay];
[self.contentView addSubview:labelPtr];
Run Code Online (Sandbox Code Playgroud)
小智 5
我正在寻找很长时间如何正确确定单元格高度, - 看起来像 - 这是一个最好的解决方案,boundingRectWithSize和constrainedToSize经常错误地计算文本高度,你需要创建UILabel而不是使用sizeThatFits函数,见下文
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, celllabelWidth, 9999)];
label.numberOfLines=0;
label.font = [UIFont fontWithName:fontName size:textSize];
label.text = @"celllabelTextHere";
CGSize maximumLabelSize = CGSizeMake(celllabelWidth, 9999);
CGSize expectedSize = [label sizeThatFits:maximumLabelSize];
return expectedSize.height;
}
Run Code Online (Sandbox Code Playgroud)
我看到了很多解决方案,但都是错误的或不完整的.您可以在viewDidLoad和autolayout中解决5行的所有问题.这对于客观C:
_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
Run Code Online (Sandbox Code Playgroud)
对于swift 2.0:
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
现在使用xib创建您的单元格或在Storyboard中创建tableview使用此功能,您无需再执行任何操作或覆盖.(不要忘记数字os行0)和底部标签(约束)降级"内容拥抱优先级 - 垂直到250"
您可以在下一个网址中下载代码:https: //github.com/jposes22/exampleTableCellCustomHeight
参考文献:http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
这很简单现在
使用以下步骤
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
享受:)
欲了解更多详情,请
访问www.raywenderlich.com
stackoverflow.com
归档时间: |
|
查看次数: |
46529 次 |
最近记录: |