【发布时间】:2012-07-17 14:08:56
【问题描述】:
我正在使用UITableView 和动态单元格,使用“右细节”样式。但是当textLabel 的宽度较大时,他会隐藏detailLabel。
我尝试调整textLabel 的大小,但它似乎无法调整大小。有没有办法在避免继承UITableViewCell 或自己添加一些UILabel 类的同时做到这一点?
【问题讨论】:
标签: iphone objective-c ios5 uitableview
我正在使用UITableView 和动态单元格,使用“右细节”样式。但是当textLabel 的宽度较大时,他会隐藏detailLabel。
我尝试调整textLabel 的大小,但它似乎无法调整大小。有没有办法在避免继承UITableViewCell 或自己添加一些UILabel 类的同时做到这一点?
【问题讨论】:
标签: iphone objective-c ios5 uitableview
在单元格中添加自定义标签。如果仍然遇到问题,请增加单元格的高度。
这是我的代码 - (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];
}
【讨论】:
您应该能够通过更改其框架来调整UILabel 的大小。或者,使用adjustsFontSizeToFitWidth 属性。请参阅文档:UILabel Documentation
【讨论】:
[cell.textLabel setFrame:CGRectMake(0, 0, 10, 30)] 但 UILabel 仍然很大。编辑:我记录了宽度并使用上面的代码我有一个 320px 宽度