0

我有一个需要看起来像这样的表格视图单元格:

在此处输入图像描述

基本上我在左侧有一个标签,它总是保持相同的长度。那没问题。在右侧,我有一个长度会改变的标签,例如它将包含某人的名字,这显然因人而异。该标签需要在单元格上右对齐,也没有问题。

问题是,在动态标签的左侧,我想要一个静态 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;
4

2 回答 2

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];

我确信有更清洁的解决方案,但我很快就解决了这个问题。这会强制图像视图随着正确的标签动态变化。如果这不是您正在寻找的内容,请告诉我。我只是根据我掌握的信息。

于 2013-02-02T03:40:20.903 回答
0

如果您的目标是 iOS 6,我建议使用“AutoLayout”。

否则,其他答案提供的代码可以工作。

还有一个很棒的工作。

制作一个“UIButton”,将样式设置为自定义并设置图像和名称。

希望它对你有用。;)

于 2013-02-02T04:27:11.413 回答