UITableViewCell不包装文本

时间:2011-10-06 17:10:46

标签: iphone ios xcode uitableview

请查看我的代码,我的单元格标签不会包装文本。

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyCellIdentifier = @"MyCellIdentifier";


    UITableViewCell *cell = (UITableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyCellIdentifier] autorelease];


        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setClipsToBounds:NO];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UILabel *label = [[UILabel alloc] init];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setNumberOfLines:0];
        [label setTextColor:[UIColor darkGrayColor]];
        [label setShadowColor:[UIColor whiteColor]];
        [label setShadowOffset:CGSizeMake(0, 1)];
        [[cell contentView] addSubview:label];
        [label release];

    }
    UILabel *froglabel = (UILabel *)cell;  
    NSUInteger row = [indexPath row];
    CGSize textSize;
    CGSize labelSize = { 100, 20000 };
    [froglabel setText:genus];
    [froglabel setFont:detailFont];
    [froglabel setTextColor:[UIColor whiteColor]];
    textSize = [[froglabel text] sizeWithFont:[self detailFont] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];

索引

处的行的表视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    CGSize textSize;
    CGSize labelSize = { 300, 20000 };  // width and height of text area


    textSize = [[self genus] sizeWithFont:[self detailFont] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%i = height %f", row, textSize.height);
    return textSize.height + 7;
    break;

enter image description here

2 个答案:

答案 0 :(得分:2)

你需要改变这个

[label setNumberOfLines:2];

它基本上告诉标签最大变形线

你还需要确保框架高度足够大2(或更多)行

答案 1 :(得分:2)

我认为这一行是罪魁祸首

UILabel *froglabel = (UILabel *)cell;  

您基本上将单元格转换为标签,这意味着您使用不推荐的方法将文本分配给单元格默认文本标签。您应该考虑以下事项:

UILabel *label;

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyCellIdentifier] autorelease];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setClipsToBounds:NO];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    label = [[[UILabel alloc] init] autorelease];

    [label setBackgroundColor:[UIColor clearColor]];
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setNumberOfLines:0];
    [label setTextColor:[UIColor darkGrayColor]];
    [label setShadowColor:[UIColor whiteColor]];
    [label setShadowOffset:CGSizeMake(0, 1)];
    label.tag = 2;
    [[cell contentView] addSubview:label];
}
else
{
    label = (UILabel *)[cell.contentView viewWithTag:2];
}

NSUInteger row = [indexPath row];
CGSize textSize;
CGSize labelSize = { 100, 20000 };
[label setText:genus];
[label setFont:detailFont];
[label setTextColor:[UIColor whiteColor]];

希望这有帮助。