2011-11-03 47 views
1

我有下面的代碼應該把圖像放在頂部的部分/單元格,然後在其他文字。我認爲問題是代碼試圖重新使用這個單元格,但是我無法弄清楚我應該改變什麼。你可以在這裏看到了問題的一個視頻:UITableView重繪沒有圖像和單元格被取代

http://screencast.com/t/OoQWMI5zCKVc

的代碼如下:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifierText = @"TextCell"; 
    static NSString *CellIdentifierImage = @"ImageCell"; 

    if (indexPath.section == 0) 
    { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierImage]; 

     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierImage] autorelease]; 
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,180)];     
      imageView.tag = 1; 
      imageView.image = [UIImage imageNamed:@"prem.jpg"]; 
      [cell addSubview:imageView]; 
      [imageView release]; 
     } 
     return cell; 
    } 
    else { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierText]; 
     if (cell == nil) 
     { 

      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifierImage] autorelease]; 
     }  
     if(indexPath.section == 1)   
      cell.textLabel.text = [arryTableData objectAtIndex:indexPath.row];  
     else if(indexPath.section ==2) 
      cell.textLabel.text = [arryAddressInfo objectAtIndex:indexPath.row]; 
     else if(indexPath.section == 3)   
      cell.textLabel.text = [arryTableActions objectAtIndex:indexPath.row];  
     else if(indexPath.section == 4) 
      cell.textLabel.text = @"REPLACE ME WITH ICONS"; 

     return cell; 
    } 
} 

回答

2

當你創建一個UITableViewCell將被顯示文本的新實例,您可以使用錯誤標識:

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:**CellIdentifierImage**] autorelease]; 

這導致了已填充文本(但沒有圖像)你的細胞之一,由0123緩存爲CellIdentifierImage標識符。所以當你問(在第0節)一個帶有這個標識符的單元格時,你實際上得到了一個只有文本填充的背單元。

+0

+1你打敗了我:) – Benjie

+0

你甜心!非常感謝,看了很長時間 – TMB87

+0

哈哈沒問題,有時你只需要一雙新鮮的眼睛。 –