2013-02-02 41 views
0

我有一个需要像这样一个实现代码如下细胞:动态的UILabel - UIImage的 - 的UILabel下令设置

enter image description here

基本上我有左侧,将始终保持相同长度的标签。这没有问题。在右边我有一个标签,将在长度发生变化,比如它包含这显然从因人而异某人的名字。该标签需要与单元格对齐,也没有问题。

的问题是,在动态标签的左边,我想有一个静态的UIImage,这将是说从动态标签10个像素,但也跟着移动。

想法?

这是当前的代码,该图像是该标签的一个子视图,但显然不是动态移动。

  // Add text label 
     UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 0.0, 80.0, 45.0)]; 
     textLabel.backgroundColor = [UIColor clearColor]; 
     textLabel.text = @"Account"; 
     textLabel.opaque = NO; 
     textLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6]; 
     textLabel.font = [UIFont boldSystemFontOfSize:15]; 
     textLabel.shadowColor = [UIColor whiteColor]; 
     textLabel.shadowOffset = CGSizeMake(0.0, 1.0); 

     //Add the image 
     UIImage *image = [UIImage imageNamed:@"image.png"]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 13.5, 18, 18)]; 
     [imageView image]; 

     // Add the Next Value label 
     UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 0.0, 100, 45.0)]; 
     valueLabel.textAlignment = UITextAlignmentRight; 
     valueLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6]; 
     valueLabel.backgroundColor = [UIColor clearColor]; 
     valueLabel.opaque = NO; 
     valueLabel.font = [UIFont systemFontOfSize:15]; 
     valueLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"name"]; 

     [cell addSubview:textLabel]; 
     [cell addSubview:valueLabel]; 
     [valueLabel imageView]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+0

显示你迄今为止尝试过的东西。 Stackoverflow不是一个代码写入服务。 – Madbreaks

+1

我完全知道Stackoverflow是什么。上面的代码显然是不工作的,并没有真正影响到的答案,但我张贴反正 – JeffN

回答

1

试试这个代码:

// Add text label 
    // same as before 
    // ... 

    NSInteger bufferFromEndOfCell = 10; // how much space you want between the end of your valueLabel and the end of the tableCell 
    NSInteger widthOfValueLabel = 100; // dynamic, change this to whatever width you want 
    NSInteger startingXlocationForValueLabel = tableView.frame.size.width - bufferFromEndOfCell - widthOfValueLabel; 

    // Add the Next Value label 
    UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(startingXlocationForValueLabel, 0.0, widthOfValueLabel, 45.0)]; 
    valueLabel.textAlignment = UITextAlignmentRight; 
    valueLabel.textColor = [UIColor colorWithRed:143/255 green:143/255 blue:143/255 alpha:.6]; 
    valueLabel.backgroundColor = [UIColor clearColor]; 
    valueLabel.opaque = NO; 
    valueLabel.font = [UIFont systemFontOfSize:15]; 
    valueLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"name"]; 

    //Add the image 
    NSInteger widthOfImage = 18; 
    NSInteger bufferBetweenImageAndLabel = 8; // the gap between your UIImageView and your valueLabel 
    CGRect rectForImageView = CGRectMake(valueLabel.frame.origin.x - widthOfImage - bufferBetweenImageAndLabel, 13.5, widthOfImage, 18); 
    UIImage *image = [UIImage imageNamed:@"image.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectForImageView]; 
    imageView.image = image; 

    [cell addSubview:textLabel]; 
    [cell addSubview:valueLabel]; 
    [cell addSubview:imageView]; 

我相信有清洁的解决方案,但我只是捣烂了这一点很快。这会强制图像视图通过正确的标签动态更改。如果这不是你正在寻找的东西,请告诉我。我只是基于我所掌握的信息。

+0

这是一个很好的解决方案,谢谢! – JeffN

+0

没有问题!很高兴工作! – Firo

0

我建议“自动版式”如果你的目标的iOS 6

其他明智的,对方的回答提供的代码可以工作。

还有一个真棒变通。

做出“的UIButton”,设置样式为自定义,并设置图像和名称。

希望它适用于你;)

+0

针对iOS 5,但感谢您的建议! – JeffN