2

cellForRowAt ..のtableviewセルのbackgroundImageをどこに設定すればよいですか?これは私がやった方法で、ちょっとうまくいきますが、backgroundimage()を設定する正しい方法と、その「if」のcell.backgroundView内側または外側の正しい場所を知りたいです。if(cell==nil)

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

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

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

    return cell;
}
4

4 に答える 4

6

以前はそうしていましたが、最近はwillDisplayCellを好みます:

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

ドキュメンテーションによると

テーブル ビューは、 cell を使用して行を描画する直前に、このメッセージをデリゲートに送信します。これにより、デリゲートはセル オブジェクトを表示する前にカスタマイズできます。このメソッドにより、デリゲートは、テーブル ビューによって以前に設定された状態ベースのプロパティ(選択や背景色など) をオーバーライドする機会が与えられます。デリゲートが戻った後、テーブル ビューはアルファとフレームのプロパティのみを設定し、スライド インまたはスライド アウト時に行をアニメーション化するときにのみ設定します。

cellForRowAtIndexPath を使用すると、セルのカスタマイズが思い通りに機能しないという奇妙な特殊なケースがいくつかありました。willDisplayCell ではそのような問題はありません。

于 2012-12-11T21:59:26.753 に答える
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath {  
[cell.imageview setImage:[UIImage imageNamed:@"image1.png"]];
   }
return cell;

}

于 2012-12-12T04:32:22.083 に答える
0

これを試してみると役立つかもしれません...

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

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

         UIImageView *backimg = [[UIImageView alloc] init];
        backimg.frame = CGRectMake(0, 0, 320, 44);
        backimg.tag = 20;
        [cell.contentView addSubview:backimg];
    }
    UIImageView *imag = (UIImageView *)[cell.contentView viewWithTag:20];
    [imag setImage:[UIImage imageNamed:@"sky.png"]];
    return cell;
}
于 2012-12-12T05:38:50.330 に答える
-1

セルの背景ビューを設定する最適な場所は、tableView:willDisplayCell:forRowAtIndexPath: メソッドです。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];
}
于 2012-12-12T00:08:01.727 に答える