2013-02-19 12 views
0

我的問題是,並非所有單元格都正確使用正確的UIView作爲渲染圖像進行初始化。(請參閱下面的代碼)在iPhone 4上,它始終與iPhone上的視網膜顯示器同一個cel也是其他細胞。在這個設置中,setNeedsDisplay將不起作用。 如果我在IBoutlet中使用相同的結構,它就可以工作。 ! 我需要在一些單元格中使用圖像文件.png和其他一些單元格定義的繪圖方法,它使用drawrect或更好的我應該使用setNeedsDisplay方法.....SetNeedsDisplay在表格視圖

什麼是錯了! !

我的代碼

的tableview ...細胞

static NSString *CellIdentifier = @"TypeCell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    CellTypes *cellType = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = cellType.title; 
    cell.detailTextLabel.text = cellType.detail; 

    if ([cellType.type rangeOfString:@"TL"].location != NSNotFound) { 
     cell.imageView.image = [UIImage imageNamed:[cellType.type stringByAppendingString:@"Thumb"]]; 
     cell.indentationWidth = 10; 

    } 
    else { 

     static CGFloat scale = 0.0; // old API , screen values 
     UIScreen *screen = [UIScreen mainScreen]; 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) { 
      scale = [screen scale]; 
     } 
     if (scale>0.0) { 
      UIGraphicsBeginImageContextWithOptions(cell.imageView.bounds.size, NO, scale); 
     } 
     else { 
      UIGraphicsBeginImageContext(cell.imageView.bounds.size); 
     } 

     CGRect imageRect = CGRectMake(0, 0, 84, 84); // cell.imageView.bounds; 

     FittingImageView *fittingImage = [[FittingImageView alloc] initWithFrame:imageRect]; 
     fittingImage.thumb = YES; 
     fittingImage.title = offSetType.title; 

     [fittingImage drawRect:imageRect]; // this works but skips one imageRect 
//  [fittingImage setNeedsDisplay]; //this won't work. 
     [cell.layer renderInContext:UIGraphicsGetCurrentContext()]; 

     cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

    } 
    return cell; 

回答

0

你永遠無法調用drawRect:自己。它被Cocoa調用,只有Cocoa可以設置上下文。目前還不清楚你想在這裏做什麼。我不知道FittingImageView是什麼。你正在創建它,然後在任何情況下拋棄它。

然後嘗試將單元格本身渲染爲圖像,並將該圖像放入單元格的圖像視圖中。這沒有意義。

您可能會誤解如何創建,重用和繪製tableview單元格。您應該重新閱讀"Creating and Configuring a Table View."

主要的事情要記住:

  • 在這個程序中,你的任務是創建一個新的小區,或者重新配置可重複使用的電池(如果dequeueReusableCellWithIdentifier:回報你的東西)。
  • 此例程不適用於繪圖單元格。這將在很晚之後纔會發生,並且繪製自己的單元格是它的工作。你只是配置該單元格。把所有事情都放在裏面,以後再畫出來。
  • 你通常不想在這個例程中計算昂貴的東西。計算其他地方並緩存它們。這個例程應該配置一個單元,將數據放入它並返回。如果您嘗試在此例程中創建新圖像,則表格視圖將會出現斷斷續續的情況。
  • 你不應該在這個例程中弄亂單元格的圖層。如果你需要一些複雜的東西,你應該用自己的drawRect:創建一個自定義單元。
+0

此外,如果FittingImageView就像是一個UIImageView任何事情,你只應在創建單元創建這些 - 你不需要添加一個'時給dequeueReusableCellWithIdentifier'你指已經有它。你確定你不想'[cell addSubview:fittingImage];'? – 2013-02-19 23:16:17

+0

嗨,fittingImage是一個UIView類,它繪製幾行直線圈出,中風和填充模式。我知道我不應該使用直角,並且正在選擇正確的方式來繪製物品。但是fittingImage取決於每個單元的值。我應該使用比[cell addSubView:fittingImgae]; – 2013-02-20 16:21:49

+0

正確。傳遞適合圖像需要的任何數據,並在請求時讓它自行繪製。 – 2013-02-21 03:01:40