0

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

4

2 回答 2

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

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

于 2013-09-12T11:39:45.513 回答
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/

于 2014-11-18T10:47:33.077 回答