2

背景のあるテキストラベルを使用していますcell.accessoryView。を選択するTableと、アクセサリビューの背景(label.backgroundColor)が消えます。を設定することで、ラベルのテキストの色を復元できますlabel.highlightedTextColor。しかし、背景色を復元できませんでした。またはのようなものはありますhighlightedTextBackgroundcolorselectedBackgroundView

UIImage *indicatorImage = [UIImage imageNamed:DISTANCE_BUBBLE];
cell.accessoryView = [[UIImageView alloc]initWithImage:indicatorImage];


distanceLabel=[[UILabel alloc]initWithFrame:cell.accessoryView.bounds];

distanceLabel.textColor=[UIColor whiteColor];
distanceLabel.textAlignment=UITextAlignmentCenter;
distanceLabel.font=[UIFont boldSystemFontOfSize:13.0];
distanceLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:DISTANCE_BUBBLE]];
distanceLabel.numberOfLines = 0;
distanceLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.accessoryView=distanceLabel;
4

3 に答える 3

1

1つのビューの背景とラベルを含むビューとしてアクセサリビューを作成します。

于 2013-01-07T09:12:15.893 に答える
1

サブクラスUITableViewCell。

それはあなたにはるかに多くの制御を与え、あなたのcellForRowAtIndexPathメソッドをよりきれいに保ちます。

于 2013-01-07T09:36:19.530 に答える
0

次のコードを書くと役立つかもしれません。

////////////////////////////////////////////////// ////////////// 編集されたコード////////////////////////////////// ////////////////////////////////////////////

セルアクセサリビューのbackGroundColorを表示する場合

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textColor = [UIColor blackColor]; 
    }
    UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [colorView setImage:image];

    [cell setAccessoryView:colorView];

    cell.textLabel.text=[self.listOfFrnd objectAtIndex:indexPath.row];

    return cell;
}

セルアクセサリビューのセルBackGround画像を表示する

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textColor = [UIColor blackColor];
    }
    UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);

    UIGraphicsEndImageContext();

    [colorView setImage:[UIImage imageNamed:@"apple.png"]];

    [cell setAccessoryView:colorView];


    cell.textLabel.text=[self.listOfFrnd objectAtIndex:indexPath.row];

    return cell;
}

試したところ、複数のセルのaccessoryViewを取得すると、 accessoryViewが1つだけ表示されることがわかりました。私が正しいかどうかはわかりませんが、本当かもしれません:)ありがとう:)

于 2013-01-07T06:34:41.563 に答える