次のように、 cellForRowAtIndexPath 呼び出し内で UITableViewCell の背景を常に設定しています。
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:simple];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id findCell in nib )
{
if ( [findCell isKindOfClass: [CustomCell class]])
{
cell = findCell;
}
}
UIView *cellBackView = [[UIView alloc] initWithFrame:CGRectZero];
UIView *cellSelectedBackView = [[UIView alloc] initWithFrame:CGRectZero];
if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_shadows_ipad_light.png"]];
cellSelectedBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"blue_cell.png"]];
}else {
cellBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"cell_shadows.png"]];
cellSelectedBackView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"blue_cell.png"]];
}
cell.backgroundView = cellBackView;
cell.selectedBackgroundView = cellSelectedBackView;
これを実現する別の方法があることを発見しました。それは、このデリゲート内に背景ビューを設定することです。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
// load the background image and set it as the background color as above
}
また、iOS5 以降では、nib を tableView に登録するだけでよいため、クラス nib を見つけるために循環する必要もありません。したがって、viewDidLoad では次のようになります。
[self.tableView registerNib:[UINib nibWithNibName: @"CustomCellSplit" bundle:nil] forCellReuseIdentifier: @"CustomCellId"];
したがって、これは機能し、より簡単であり、セルのペン先をロードして背景ビューを設定する Apple 推奨の方法です。ただし、リストをスクロールすると、この tableView: willDisplayCell: forRowAtIndexPath が行ごとに呼び出されることがわかりました。前に背景ビューを読み込んだ方法では、セルが作成されたときに backgroundView のみが設定されます (最大で 8 回または 10 回)。
したがって、新しい方法は、セルをロードして backgroundViews を設定するパフォーマンスの低い方法のように思えます。これは正しい仮定ですか、それともここで何か不足していますか?