ios:将带有标签的细胞贴合到标签的大小

时间:2013-09-12 11:28:21

标签: ios uitableview size label cell

我有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项目,这是让屏幕看起来像我想要的最佳方法吗?

2 个答案:

答案 0 :(得分:0)

- (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);
}

考虑一个字符串,它具有最大高度,然后适合单元格的高度。我认为根据您的要求实现的基准字符串

答案 1 :(得分:0)

我是从我的同事(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];

....

}

但我建议您使用http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/

相关问题