11

UITableViewCell の imageView 内にさまざまなサイズの画像を配置しようとしています。画像データを非同期に取得し、画像を作成し、imageView のコンテンツ モードを設定し、最後に imageView の境界を設定します。しかし、コードは私が行った変更に影響されないようです。画像を 75x75 の領域の中央に配置したい。この目的のために以下のコードを書きました

UIImage* image = [UIImage imageWithData:data];
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw];
[holder.imageView setImage:image];
[holder.imageView setBounds:CGRectMake(0,0,75,75)];
[holder.imageView setFrame:CGRectMake(0,0,75,75)];
[holder setNeedsLayout]; 

holder は UITableViewCell です。私が得る結果は常に同じです。すべての画像の高さは 75 ピクセルで、幅はさまざまです。誰かがこの問題を解決するのを手伝ってくれますか?

contentMode と bounds プロパティを設定しても、そのコードでは何の効果もないことに気付きました。最後の行の後に NSLog を追加したところ、次のような結果が得られました。

NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);

imageview:<UIImageView: 0x39ab8a0; フレーム = (0 0; 75 75); 不透明 = いいえ; userInteractionEnabled = NO; layer = <CALayer: 0x39a92b0>> 境界と contentMode:(null) (null)

まだ解決策はありません

4

5 に答える 5

45

完了しました。最終的に解決策を見つけましたが、3時間かかりました=)

解決策は、カスタム UITableViewCell クラスの -(void)layoutSubviews メソッドで、bound、frame、contentMode などのプロパティを変更することです。「トリック」は、このメソッドにレイアウト コードを記述することです。そうしないと、コードは何の効果もありません。

以下のコードは私のために仕事をしました。テーブルの行を垂直方向に整列させます。

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.bounds = CGRectMake(0,0,75,75);
    self.imageView.frame = CGRectMake(0,0,75,75);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;

    CGRect tmpFrame = self.textLabel.frame;
    tmpFrame.origin.x = 77;
    self.textLabel.frame = tmpFrame;

    tmpFrame = self.detailTextLabel.frame;
    tmpFrame.origin.x = 77;
    self.detailTextLabel.frame = tmpFrame;

}
于 2010-06-28T11:14:37.750 に答える
11

したがって、UITableViewCellの問題は、組み込みオブジェクト(つまり、imageView、contentView、accessoryView、backgroundView)のサイズを制御できないことです。テーブルが変更されると、カスタマイズは踏みにじられます。

Behlulが指摘したように、layoutSubviewsを使用してサイズを強制的に修正することはできますが、その問題は、テーブルがスクロールするたびにlayoutSubviewsが呼び出されることです。これは、多くの不要な再レイアウト呼び出しです。

別の方法は、すべてのコンテンツをcontentViewに追加することです。同様に、背景をカスタマイズする場合は、透明なbackgroundViewを作成し、カスタムの背景ビュー(myBackgroundViewなど)をbackgroundViewのサブビューとして追加できます。

このようにして、アイテムを希望どおりに配置およびサイズ変更できます。

欠点は、アクセサリまたは画像ビューからストックメッセージが受信されなくなったことです。あなたはただあなた自身を作成する必要があります。

お役に立てば幸いです。

// This code is not tested
// MyCustomTableViewCell
- (id) init{
    self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"MyReuseIdentifier"];
    if(self){
        //image view
        my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_image.png"]] retain];
        [my_image_view setFrame:CGRectMake(10,10,30,30)];
        [self.contentView addSubview:my_image_view];

        //labels
        my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain];
        [self.contentView addSubview:my_text_label];
        //set font, etc

        //detail label
        my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain];
        [self.contentView addSubview:my_detail_label];
        //set font, etc

        //accessory view
        //Whatever you want to do here
        //attach "accessoryButtonTapped" selector to button action

        //background view
        UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease];
        [background_view setBackgroundColor:[UIColor greenColor]];
        background_view.layer.cornerRadius = 17;
        background_view.layer.borderWidth = 3;
        background_view.layer.borderColor = [UIColor whiteColor].CGColor;

        [self setBackgroundView:[[[UIView alloc] init] autorelease]];
        [self.backgroundView addSubview:background_view];
    }

    return self;
}

- (void) setLabelText: (NSString*) label_text{
    [my_text_label setText:label_text];
}

- (void) setDetailText: (NSString*) detail_text{
    [my_detail_label setText: detail_text];
}

- (void) accessoryButtonTapped{
    //call table view delegate's accessoryButtonTappedForRowWithIndexPath method
}
于 2012-02-16T21:09:57.737 に答える
2

「UIViewContentModeCenter || UIViewContentModeRedraw」は 1 に相当します。これもビットフィールドではありません。UIViewContentModeCenter が必要です。

UITableViewCell.imageView はセルによって管理されます。カスタム レイアウトが必要な場合は、ビューを contentView に追加してみてください (「75x75 領域の中央に配置する」とはどういう意味かを推測しています)。

UIImageView * iv = [[[UIImageView alloc] initWithImage:image] autorelease];
iv.frame = (CGRect){{0,0},{75,75}};
iv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin;
iv.contentMode = UIViewContentModeScaleAspectFit;
[holder.contentView addSubview:iv];
于 2010-06-28T12:54:02.170 に答える
1

imageView の「contentMode」プロパティを「UIViewContentModeScaleAspectFit」または「UIViewContentModeScaleAspectFill」に変更してみてください

于 2010-06-28T09:01:48.433 に答える