次のように、indexPath に基づいて cellForRowAtIndexPath デリゲート メソッドの背景を変更できます。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TaskCellRow";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int maxRow = 3;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]]];
}
else
{
UIImageView *background = (UIImageView *)cell.backgroundView;
background.image = [UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(indexPath.row, maxRow)]];
}
return cell;
}
// To change header backgrounds
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
int maxRow = 3;
UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat: @"background_image%i.png", MIN(section, maxRow)]]];
return headerView;
}
次に、必要な量のヘッダー/行に番号を付けた画像を作成するだけです。background_image0.png、background_image1.png、background_image2.png など。MIN は、最大バックグラウンドであると決定したものでオフ量を制限します。それが役立つことを願っています。
編集: Henri のコメントに基づいて cellForRowAtIndexPath を変更しました。私はそれを見落としました、ありがとう!これは ARC との互換性のためです。