【问题标题】:UITableViewCell background image settingUITableViewCell 背景图片设置
【发布时间】:2013-03-21 10:20:40
【问题描述】:

我尝试将背景图片添加到我的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;
}

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

【问题讨论】:

标签: ios objective-c cocoa-touch uitableview


【解决方案1】:

加载缓慢是因为您每次都将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] ];
    }

【讨论】:

    【解决方案2】:

    你需要移动

    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)... 条件之后的行。否则,单元格只有在被回收后才能获得新的背景,而新创建的单元格仍然没有背景。

    【讨论】:

      【解决方案3】:
      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];
      }
      

      【讨论】:

        【解决方案4】:
        - (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;
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-01
          • 2023-03-25
          • 1970-01-01
          • 2020-11-21
          • 2015-05-09
          • 2012-03-07
          • 2020-07-03
          相关资源
          最近更新 更多