如何使用2个不同颜色的标签制作UITableViewCell?

Dix*_*ine 6 iphone objective-c uitableview uilabel

我想要像这样的UITableViewCell:

在此输入图像描述

"Tel:"的文本颜色必须与数字的文本颜色不同.现在我像往常一样将文本设置为单元格:

cell.textLabel.text=@"Something";
Run Code Online (Sandbox Code Playgroud)

是否有可能有1个标签并改变其某些部分的颜色?

如何在我的照片上制作表格?

谢谢.

Meh*_*tri 8

您应该在Cell中使用两个标签...并在UITableViewCell的子视图中添加这两个Label

在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;
}
Run Code Online (Sandbox Code Playgroud)

更新:

除此之外,您还可以在此处定义自定义单元格

快乐编码..


Mat*_*Mat 5

许多人使用添加标签作为单元格子视图的技术,并且可能会在重复使用单元格时遇到意外结果.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];

        cell.textLabel.text=@"Tel.:";
        cell.detailTextLabel.text=@"+3912345678";

        UIImageView *imageView = [[UIImageView alloc] initWithImage:
                                  [UIImage imageNamed:@"phone.png"]];
        cell.accessoryView = imageView;
        [imageView release];
        return cell;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.