3

に背景画像を追加しようとしていUITableViewCellます。これは私が使用しているコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"TableCell";

    // Dequeue or create a cell of the appropriate type.
    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    [cell.preparationLabel setFont:[UIFont fontWithName:@"Bold" size:20]];

    // Configure the cell.
    if (btnselected==1)
    {
        cell.preparationLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
    }
    else
    {
     cell.preparationLabel.text = [recipe.preparation objectAtIndex:indexPath.row];
    }
    return cell;
}

私の問題は、アプリを実行してテーブルビューを選択しているときに、最初にデフォルトの白い背景のテーブルセルが表示され、しばらくするとセルの背景が目的の画像に変更されることです...なぜ時間がかかるのかわかりません希望するセルの背景をテーブルビューにロードします

4

4 に答える 4

6

読み込みが遅いのはUIImageView、新しいセルを作成するときだけでなく、毎回セルにを追加しているためです。

次のようにコードを変更します。

   // Dequeue or create a cell of the appropriate type.
    TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    }
于 2013-03-21T10:24:55.423 に答える
3

あなたは移動する必要があります

cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbg.jpeg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

条件の後への行if (cell == nil)...。それ以外の場合、セルはリサイクルされた後にのみ新しい背景を取得しますが、新しく作成されたセルは背景なしのままになります。

于 2013-03-21T10:25:06.260 に答える
2
if (cell == nil) 
{
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];

        UIImageView *imgView1 = [[UIImageView alloc]init];..continue...
        UIImageView *imgView2 = [[UIImageView alloc]init];..continue...

        [cell.backgroundView addSubView:imgView1];
        [cell.selectedBackgroundView addSubView:imgView2];
        cell = [nib objectAtIndex:0];
}
于 2013-03-21T10:26:17.713 に答える
0
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    UIImageView *ivwCellHighlighted = [[UIImageView alloc]initWithImage:[UIImage imageNamed:CELL_SELECTEDIMAGE]];
    [ivwCellHighlighted setFrame:CGRectMake(1, 1, 320, 47)];
    [cell setSelectedBackgroundView:ivwCellHighlighted];
    [ivwCellHighlighted release];
    ivwCellHighlighted=nil;


}
于 2013-03-21T10:44:22.450 に答える