2

UIAppearance を使用して、アプリ全体でテーブル セルの背景画像を設定しました。

[[UITableViewCell appearance] setBackgroundView:[ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list_bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]];

ただし、リストを表示すると、背景画像は画面上のセルの 1 つにのみ設定されます。画面をスクロールすると、表示されるセルに背景画像が設定されますが、この背景画像は他の表示セルに設定されません。

何が起こっているのか誰にもわかりませんか?UITableviewCell の UIAppearance を設定することで、このタイプのすべてのインスタンスが自動的に背景画像を取得すると考えました。

ありがとう

ブライアン

これが私の cellForRowAtIndexPath メソッドです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"plantCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"PlantListCell" owner:self options:nil];
    cell = _plantCell;
    self.plantCell = nil;
}


Plant *plant = [self.fetchedResultsController objectAtIndexPath:indexPath];

UIImageView *thumbnail = (UIImageView *)[cell viewWithTag:1];
thumbnail.image = [UIImage imageNamed:[plant.name stringByAppendingString:@"_S"]];

UILabel *label = (UILabel *)[cell viewWithTag:2];
label.text = plant.name;

UIImageView *set = (UIImageView *)[cell viewWithTag:3];
set.image = [UIImage imageNamed:@"set"];

UIImageView *favourite = (UIImageView *)[cell viewWithTag:4];
favourite.image = [UIImage imageNamed:@"fav"];

return cell;
}
4

4 に答える 4

5

うん。これに関する問題は、UITableViewCellクラスに、テーブルビューセルの背景ビューごとに1つのUIImageViewを設定するように指示していることです。1つに設定されると、以前のスーパービューから削除され、新しいUITableViewCellに追加されます。各tableViewCellに重複したUIImageViewsが設定されることはありません。すべてのテーブルビューセルに1つのビューセットを取得しています。

したがって、各tableViewCellから、最後に設定されたものまで設定および設定解除されます。

このメソッドに外観プロキシを使用すると、UITableViewCellのすべてのオブジェクトにプロキシが設定されます。

渡されるビューは1つのビューになり、一度に1つのセルにしか適用できないため、外観プロキシを使用して背景ビューの方法を設定することはしません。

セルごとに個別にビューを作成するか、初期化時にセルを設定するサブクラスを作成することをお勧めします。

于 2012-04-15T01:49:24.240 に答える
2

バーコードプロジェクトに感謝​​します。それは理にかなっていますが、実際に機能しない場合、AppleがわざわざUITableviewCellの背景画像を設定できるようにした理由がわかりません。cellForRowAtIndexPath メソッドで画像を設定するだけで、最終的に簡単な解決策を取りました。

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_bg.png"]];

正常に機能しますが、テーブルがたくさんあり、一般的な方法で設定したい場合はうまくいきません。

乾杯

ブライアン

于 2012-04-15T13:26:05.837 に答える
1

CustomizableUITableCellという基本クラスを作成し、背景画像のUI外観値を設定することで、これを解決しました。次に、カスタムの背景で必要なセルを作成するときはいつでも、単純なサブクラスCustomizableUITableCellを使用します。

//ヘッダ

#import <UIKit/UIKit.h>

@interface CustomizableUITableCell : UITableViewCell <UIAppearance>

@property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR;

@end

//実装

#import "CustomizableUITableCell.h"

@implementation CustomizableUITableCell

@synthesize backgroundCellColor;

#pragma mark - Public Methods.

-(void)setBackgroundCellColor:(UIColor *)backgroundColor
{
    [super setBackgroundColor:backgroundColor];
}

@end

その後、外観プロキシを使用して設定できます。

[[CustomizableUITableCell appearance] setBackgroundCellColor:[UIColor colorWithPatternImage:backgroundImage]];
于 2012-07-15T13:17:26.790 に答える
0

cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"1.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

わたしにはできる..

于 2013-09-26T11:20:17.383 に答える