【问题标题】:textLabel hidding detailLabeltextLabel 隐藏 detailLabel
【发布时间】:2012-07-17 14:08:56
【问题描述】:

我正在使用UITableView 和动态单元格,使用“右细节”样式。但是当textLabel 的宽度较大时,他会隐藏detailLabel

我尝试调整textLabel 的大小,但它似乎无法调整大小。有没有办法在避免继承UITableViewCell 或自己添加一些UILabel 类的同时做到这一点?

【问题讨论】:

    标签: iphone objective-c ios5 uitableview


    【解决方案1】:

    在单元格中添加自定义标签。如果仍然遇到问题,请增加单元格的高度。

    这是我的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    cell.textLabel.textColor=[UIColor blackColor];
    cell.backgroundColor=[UIColor clearColor];  
    NSDictionary *dict=[dataArray objectAtIndex:indexPath.row];
    NSString *strName=[dict objectForKey:@"name"];
    NSString *strType = [dict objectForKey:@"type"];
    cell.selectionStyle = NO;
    cell.textLabel.numberOfLines=1;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    UILabel *lbl =[[UILabel alloc]init];
    lbl.frame=CGRectMake(115,8,220,40);
    lbl.text=strName;
    lbl.textColor = [UIColor colorWithRed:74/255.0f green:64/255.0f blue:34/255.0f alpha:1.0];
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        lbl.font = [UIFont boldSystemFontOfSize:17];
    }
    else
    {
        lbl.font = [UIFont boldSystemFontOfSize:15];
    }
    lbl.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
    lbl.numberOfLines=1;
    lbl.backgroundColor=[UIColor clearColor];
    
    UILabel *lbl1 =[[UILabel alloc]init];
    lbl1.frame=CGRectMake(115,35,220,40);
    lbl1.text=strType;
    lbl1.textColor = [UIColor colorWithRed:74/255.0f green:64/255.0f blue:34/255.0f alpha:1.0];
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        lbl1.font = [UIFont boldSystemFontOfSize:17];
    }
    else
    {
        lbl1.font = [UIFont boldSystemFontOfSize:15];
    }
    lbl1.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
    lbl1.numberOfLines=1;
    lbl1.backgroundColor=[UIColor clearColor];
    
    
    [[cell contentView] addSubview:lbl];
    [[cell contentView] addSubview:lbl1];
    return cell;
    
    [cell release];
    [CellIdentifier release];
    [lbl release];
    

    }

    【讨论】:

    • @keyur bhalodiya 不需要太多类型的代码,只需要精确的代码。不要直接在这里复制粘贴。
    【解决方案2】:

    您应该能够通过更改其框架来调整UILabel 的大小。或者,使用adjustsFontSizeToFitWidth 属性。请参阅文档:UILabel Documentation

    【讨论】:

    • 我做 [cell.textLabel setFrame:CGRectMake(0, 0, 10, 30)] 但 UILabel 仍然很大。编辑:我记录了宽度并使用上面的代码我有一个 320px 宽度
    • 也许你没有你认为你拥有的东西。当您将其设置为 10 时,宽度如何为 320?我不明白你的意思。
    • 你完全明白我的意思:) 我将宽度设置为 10,但是当我尝试将其记录到 consol 日志中时,宽度设置为 320!我找到了一篇关于它的帖子,似乎这个标签无法调整大小(我现在没有链接)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2013-08-10
    • 2011-12-28
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多