1

セルの高さよりも高い高さでセルを選択した画像を表示するという問題に直面しています。

サンプルは次のとおりです。

ここに画像の説明を入力

ここでは、セルの高さは 44 ですが、選択したセルの背景画像の高さは 50 です。

セルで選択した背景画像を設定しようとしましたが、新しい画像ビューをセルにサブビューとして追加することも試みました。しかし、セルが選択され、画像がセルの高さのサイズに合わせて拡大縮小されるようにすることです。その矢印がセル内に表示され、画像が圧迫されます。

私が試した2番目の方法論は、ボタンを使用して矢印の画像を分離することでしたが、そのオプションもうまくいきませんでした。この方法論を使用すると、そのボタンが表示されなくなります。ここで、その部分がテーブルビューの次の行に重なっていると思います。

このシナリオを解決する方法を教えてください。

前もって感謝します。

4

2 に答える 2

0

Clip Subviewsセルや実際のテーブルのプロパティで遊ぶことができると思います。セルに自分を入れてUIImageView(Backgroundプロパティとして割り当てることができるかどうかはわかりませんが、試すことができます)、セルの高さよりも高い高さに設定します。ただし、唯一の問題は、画像が重なる可能性があることです。選択した状態のビューを配置する実際のインデックスを設定することで、問題の解決を試みることができます。

さて、少しテストしました。このコードを見てください:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
        cell.clipsToBounds = NO;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        CGRect frame = cell.bounds;
        UIImageView *background = [[UIImageView alloc] initWithFrame:frame];
        background.image = [UIImage imageNamed:@"CellBackground"];
        cell.backgroundView = background;
        [background release];

        frame.size.height = 50.0;
        UIImageView *selected = [[UIImageView alloc] initWithFrame:frame];
        selected.tag = 13;
        selected.image = [UIImage imageNamed:@"CellSelectedBackground"];
        [cell insertSubview:selected atIndex:1];
        selected.hidden = YES;
        [selected release];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"cell %d", indexPath.row];

    return cell;
}

ここでの主なものはcell.clipsToBounds = NO;[cell insertSubview:selected atIndex:1];です。ただし、セルのサブクラスを作成し、ここですべてのものと選択を処理する方がはるかに優れています。あなたの写真で見られるように、あなたはすでにサブクラス化されたセルを使用していますが:)

于 2012-08-24T09:48:43.103 に答える
0

ご回答ありがとうございます。私はその問題を解決しました。コードは次のとおりです。

    - (id)initWithStyle:(UITableViewCellStyle)style
        reuseIdentifier:(NSString *)reuseIdentifier
    {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

      if (self) {
        // Initialization code

        [self setClipsToBounds:NO];

       // selected background image view
        if (self.selectedBgImageView == nil) {

    //     w 302  :  h 50
          self.selectedBgImageView = [[UIImageView alloc]
                                      initWithFrame:CGRectMake(0, -5, 302, 50)];
        }
      [self.selectedBgImageView setContentMode:UIViewContentModeScaleToFill];    
        [self.contentView addSubview:self.selectedBgImageView];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];

  // Configure the view for the selected state

  // child count label
  [self.childCountLabel setBackgroundColor:
    [UIColor colorWithPatternImage:
    [UIImage imageNamed:@"btn_contacts_network_total"]]];

  // selected background image view
  if (self.indentationLevel % 2 == 0) {

    [self.selectedBgImageView setImage:
     [UIImage imageNamed:@"img_contacts_network_expand_box_tier1"]];
  }
  else {

    [self.selectedBgImageView setImage:
     [UIImage imageNamed:@"img_contacts_network_expand_box_tier2"]];
  }

  [self.selectedBgImageView setContentMode:UIViewContentModeScaleToFill];
//  [self.con:selectedBgImageView];
}

そこで、フレームの設定が問題であることがわかりました。フレームの原点のこのような奇妙な計算の理由がわかりません。しかし、どういうわけかそれは私にとってはうまくいき、要件に従って完全に機能しました.

この動作の背後にある理由を誰かが知っている場合は、私を導いてください。

于 2012-08-26T06:00:27.273 に答える