iOSアプリ開発の逆引き辞典

iPhone/iPadで使えるアプリ開発のTipsをまとめてみました

UITableViewに背景画像を設定する

UITableViewに背景を設定してみましょう。

画像をソースにしてパターン画像を作って(UIColor colorWithPatternImageメソッド)、設定すると駄目にスクロールさせた時にセルの表示が残念な事になってしまいます。

tableView:UITableView:cellForRowAtIndexPath:NSIndexPathメソッドで、cellのtextLabelのbackgroundColorプロパティに透過色を設定します。これでセルの背景は透明となります。

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.backgroundColor = [UIColor clearColor];
}

viewDidLoadメソッドで、TableViewの画像を任意の背景画像へ設定します。

// 背景に画像を設定する
UIImage* image = [UIImage imageNamed:@"woo_001.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
[self.tableView setBackgroundView:imageView];
[imageView release];

これで一通り完了。あとは、見栄えを良くするためのシャドーを設定しておきましょう。tableView:UITableView:cellForRowAtIndexPath:NSIndexPathメソッドで、以下のような設定をおこないます。

cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0);

f:id:ch3cooh393:20140226153142p:plain