0

UILabelをテーブルセルに追加するには、

UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.frame = CGRectIntegral(timeLabel.frame);
[cell.contentView addSubview:timeLabel]; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

これは、テーブルをスクロールするかセルを選択するまでは正常に機能します。次に、ラベルがピクセル化されます。

ロード時:ここに画像の説明を入力してください

アクション後:ここに画像の説明を入力してください

また、 UITableViewCellをサブクラス化してラベルを追加し、にロード しようとしました- (void) layoutSubviews

私はすでにここここで関連する質問を見つけましたが、何も機能しませんでした。

編集:標準のセルラベルはすでに使用されているため、使用できません。ラベルを追加する必要があります。

4

1 に答える 1

0

私はついにそれを汚い修正で動作させました。

In- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

設定しました

cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITableViewCellのサブクラスで、次のようにinitWithStyleにtimeLabelをロードします。

timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor whiteColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:timeLabel];

次に、これら2つの関数をオーバーライドします。

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if(highlighted == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        //scale custom cell background to necessary height
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        //set cell background
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        //set textcolor for default labels
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        //set textcolor for custom label
        timeLabel.textColor = [UIColor whiteColor]; 
        //cope background for custom label background since timeLabel.backgroundColor = [UIColor clearColor] doesnt work
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];
        //set custom label background
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        //set unselected colors
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        //white background works without the label pixelates
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        timeLabel.textColor = [UIColor whiteColor];
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];  
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

これが何人かの人々に役立つことを願っています!

于 2012-08-12T15:34:53.287 に答える