UITableViewCell背景图像设置

Vor*_*ork 3 cocoa-touch objective-c uitableview ios

我尝试将背景图片添加到我的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;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,在运行应用程序并选择tableview时,它首先显示具有默认白色背景的表格单元格,并且在一段时间之后单元格背景将变为所需图像...我不知道为什么需要很长时间才能使用我想要的单元格背景加载表格视图

Pet*_*tar 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] ];
    }
Run Code Online (Sandbox Code Playgroud)