我想有UITableViewCell的是这样的:如何制作带有2种不同颜色标签的UITableViewCell?
“电话:”的文本颜色必须是从一些文字的颜色不同。现在我像往常一样设置文本到单元格:
[email protected]"Something";
是否有可能有1个标签并更改其某些部分的颜色?
如何使表格单元格像我的照片?
谢谢。
我想有UITableViewCell的是这样的:如何制作带有2种不同颜色标签的UITableViewCell?
“电话:”的文本颜色必须是从一些文字的颜色不同。现在我像往常一样设置文本到单元格:
[email protected]"Something";
是否有可能有1个标签并更改其某些部分的颜色?
如何使表格单元格像我的照片?
谢谢。
你应该要采取两种标签,在单元格...在UITableViewCell中
的子视图中的cellForRowAtIndexPath方法中添加这两种标签写这篇文章。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for(UIView *eachView in [cell subviews])
[eachView removeFromSuperview];
//Initialize Label
UILabel *lbl1 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl1 setFont:[UIFont fontWithName:@"FontName" size:12.0]];
[lbl1 setTextColor:[UIColor grayColor]];
lbl1.text = YOUR CELL TEXT;
[cell addSubview:lbl1];
[lbl1 release];
UILabel *lbl2 = [[UILabel alloc]initWithFrame:YOURFRAME];
[lbl2 setFont:[UIFont fontWithName:@"FontName" size:12.0]];
[lbl2 setTextColor:[UIColor blackColor]];
lbl2.text = YOUR CELL TEXT;
[cel2 addSubview:lbl2];
[lbl2 release];
return cell;
}
UPDATE:
随着这个你也可以创建自定义单元格的定义here
编码快乐..
'for(UIView * eachView in [cell subviews])[eachView removeFromSuperview];'太棒了。对于那个 –
'for(UIView * eachView in [cell subviews] .copy)''是更安全的。另一种形式崩溃(或用于在以前的可可的崩溃)。 –
cell.textlabel? – SampathKumar
唯一的方法是添加新的标签作为子视图与不同的颜色作为他们的背景。
要么与自定义单元格一起按照需求进行初始化,要么可以使用UITableviewCell,并根据需要的格式添加多个UILabel。
例如...,
static NSString *MyIdentifier [email protected]"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier];
}
UILabel *lblPhone = [[UILabel alloc] initwithFrame:CGRectMake(5, 5, 150, 30)];
lblPhone.text = @"Tel.";
[cell addSubView: lblPhone];
您不应该将子视图添加到单元格中,而是添加到单元格的内容视图中,以便编辑时的行为是合理的。此外,这将需要调整默认textLabel的框架,以便没有重叠。 – jbat100
这是不能够改变字体或颜色为一个UILabel的部分。最简单的方法是创建一个单元格子类,并添加两个标签或者界面构建器或代码。 This post向您展示了如何计算标签中文字的大小,以便您可以动态调整标签的大小,如果您希望两个标签的文字“打扮”。
许多使用添加标签作为子视图的技术该单元格,并且可能会遇到意外的结果,并重新使用这些单元格。 Apple已经提供了可以在各个方面进行定制的模板。你可以使用模板UITableViewCellStyleValue2
,你可以使用现有的标签和accessoryView,如cell.textLabel
,cell.detailTextLabel
和cell.accessoryView
,请参阅下面的代码段,它或多或少的模拟你的界面:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellID = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.contentMode=UIViewContentModeScaleToFill;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
cell.textLabel.textAlignment=UITextAlignmentCenter;
cell.textLabel.font=[UIFont boldSystemFontOfSize:22];
cell.textLabel.textColor=[UIColor lightGrayColor];
cell.detailTextLabel.contentMode=UIViewContentModeScaleToFill;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
cell.detailTextLabel.textAlignment=UITextAlignmentLeft;
cell.detailTextLabel.font=[UIFont boldSystemFontOfSize:23];
cell.detailTextLabel.textColor=[UIColor blackColor];
[email protected]"Tel.:";
[email protected]"+3912345678";
UIImageView *imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"phone.png"]];
cell.accessoryView = imageView;
[imageView release];
return cell;
}
希望这会有所帮助。
你总是可以继承UITableViewCell和两个UILabel。 – tilo