ios:将带有标签的单元格调整为标签的大小
我有一个ios:将带有标签的单元格调整为标签的大小,ios,uitableview,size,label,cell,Ios,Uitableview,Size,Label,Cell,我有一个tableview并将数据宽度加载到一个json数组中。 我所做的是,我在每个表视图单元格中创建两个UILabel。 这是工作和[label sizeToFit]。 我现在的问题是设置保存这两个标签的单元格的大小。我试图设置[cell sizeToFit] 但这不起作用。 有人能帮我解决这个问题吗? 代码如下: NSString * titel = [[json_Array objectAtIndex:indexPath.row ] objectForKey:@"maintitel"];
tableview
并将数据宽度加载到一个json数组中。
我所做的是,我在每个表视图单元格中创建两个UILabel
。
这是工作和[label sizeToFit]代码>。
我现在的问题是设置保存这两个标签的单元格的大小。我试图设置[cell sizeToFit]代码>
但这不起作用。
有人能帮我解决这个问题吗?
代码如下:
NSString * titel = [[json_Array objectAtIndex:indexPath.row ] objectForKey:@"maintitel"];
NSString * datum = [[json_Array objectAtIndex:indexPath.row] objectForKey:@"datum"];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-mm-dd"];
NSDate *date = [dateFormat dateFromString:datum];
// Convert date object to desired output format
[dateFormat setDateFormat:@"dd.mm.yy"];
NSString * dateStr = [dateFormat stringFromDate:date];
NSString * datum1 = [dateStr description];
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 80, 50)];
label.text = datum1;
UILabel *label1 = [[UILabel alloc] initWithFrame: CGRectMake(80, 0, 245, 50)];
label1.text = titel;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.userInteractionEnabled = NO;
label1.numberOfLines = 0;
label1.lineBreakMode = UILineBreakModeWordWrap;
label1.userInteractionEnabled = NO;
[label sizeToFit];
[label1 sizeToFit];
[cell.contentView addSubview:label];
[cell.contentView addSubview:label1];
我的第二个问题是因为我几乎是iOS开发新手,但已经完成了一些android项目,这是让屏幕看起来像我想要的最好方法吗
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString * datum = [[json_Array objectAtIndex:indexPath.row] objectForKey:@"datum"];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [datum sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 77.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
假设一个字符串具有最大高度,然后适合单元格的高度。我考虑了根据您的要求实现的基准字符串我从我的同事(ANIRUDH)那里得到的,工作正常,可能会有帮助:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Calculating height on base of length of text
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:YOURSTRING //[NSString string withformat:@"\n Yourstring"] \n for: number of lines require for static label in cell
attributes:@
{
NSFontAttributeName: font
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){CELL_CONTENT_WIDTH, CGFLOAT_MAX}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
// NSString *height = [NSString stringWithFormat: @"%.2f", ceilf(size.height)+22];
return (ceilf(size.height)+22)*0.6; //change 0.6 to 0.9 according to the difference in required and extra calculted height, it works for me every time
}
而且
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.....
label.numberOfLines = 0;
label.text = YOURTEXT;
[label sizeToFit];
....
}
但我建议使用可以使用自定义单元格。为此,他们感谢我在设置最大值(size.height,50)时部分工作正常。但我不知道最大值中的第二个值是什么意思?