2013-03-21 154 views
3

我嘗試將背景圖像添加到我的UITableViewCell。 這是我使用的代碼: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; 
} 

我的問題是,當運行應用程序並選擇實現代碼如下,首先顯示了默認的白色背景和一定的時間單元格背景後的表格單元格將得到改變的渴望圖像......我不知道爲什麼它需要很長時間來加載表格視圖與我想要的單元格背景

+0

參考http://stackoverflow.com/a/15353155/1713478回答它會幫助你 – Pratik 2013-03-21 10:38:27

回答

5

加載緩慢是因爲您每次都將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] ]; 
    } 
3

您需要if (cell == nil)...條件後移動

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
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]; 
} 
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; 


}