2012-02-02 25 views
1

我在我的UITableViewCell子類中有一個DotImageView的屬性。在cellForRowAtIndexPath中的UITableViewCell的子類中顯示UIImageView

@property (nonatomic, retain) IBOutlet UIImageView *DotImageView; 

我嘗試cellForRowAtIndexPath方法中是這樣顯示的:

static NSString *TargetCellIdentifier = @"TargetDetailTableViewCellIdentifier"; 
    TargetDetailTableViewCell *cell = (TargetDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:TargetCellIdentifier]; 
if (cell == nil) { 
    UINib *nib = [UINib nibWithNibName:@"TargetDetailTableViewCell" bundle:nil]; 
    [nib instantiateWithOwner:self options:nil]; 
    cell = self.TargetCell; 
    self.TargetCell = nil; 
} 
NSUInteger row = [indexPath row]; 
NSInteger section = [indexPath section]; 

Target *aTarget = [targetDictionary objectForKey:[NSNumber numberWithInteger:section]]; 

if (row == 0) { 
     UIImage *dot; 
     if (aTarget.importance == high) { 
      dot = [UIImage imageNamed:@"dot_red.png"]; 
     } 
     else { 
      dot = [UIImage imageNamed:@"dot_gray.png"]; 
     } 
     UIImageView *variantImageView = [[UIImageView alloc] initWithImage:dot]; 
     cell.DotImageView = variantImageView; 
     [variantImageView release]; 
     return cell; 

我沒有看到我的細胞的任何圖像。我的UITableViewCell的子類的其他標籤顯示。我用UIImageView丟失了什麼?我並不是非常熟悉它。謝謝。

+0

cell.DotImageView它的創建?也許你忘了在xib – Edig 2012-02-02 16:16:54

+0

中連接它,當你說:「我在我的UITableViewSubclass中有一個DotImageView的屬性」它是UITableViewCell?我認爲我們需要在cellForRowAtIndexPath方法中看到你的UITableViewCell聲明。 – TheRonin 2012-02-02 16:18:45

+0

@Ernesto我檢查了筆尖,它看起來連接 – Crystal 2012-02-02 16:25:45

回答

0

改變你的代碼

if (row == 0) { 
     UIImage *dot; 
     if (aTarget.importance == high) { 
      dot = [UIImage imageNamed:@"dot_red.png"]; 
     } 
     else { 
      dot = [UIImage imageNamed:@"dot_gray.png"]; 
     } 
     cell.DotImageView.image = dot; 
     return cell; 

你正在做的是你要分配新的UIImageView對象cell.DotImageView現在你cell.DotImageView指向這個局部圖像視圖對象還沒到那個對象,它是廈門國際銀行上

+0

他將單元格綁定到視圖控制器上名爲TargetCell的出口,然後將單元格加載到該屬性中。這是一種有效的方法,比objectAtIndex版本更安全一些(例如,如果單元格不是nib中的第一個對象,例如),但我同意,您的方法少了很多代碼,並且工作起來也一樣在多數情況下。我不認爲這就是爲什麼他的代碼不起作用。 – 2012-02-02 16:53:11

+0

再次編輯.... – 2012-02-02 16:57:29

0

我得到了它與

[cell.DotImageView setImage:dot]; 

工作,我不知道爲什麼它的工作原理VS其他方法我本來。

+0

它不起作用,因爲當你替換單元格的imageView時,你還需要添加新的imageView作爲單元格的子視圖,否則它只是單元格的一個屬性,它是實際上並不在視圖層次結構中。說實話,我會堅持後一種方法 - 更容易。 – 2012-02-02 16:55:27

0

1 - 基本情況

只需使用標準的UITableViewCell的圖像性能。它適用於大多數情況。

2 - 自定義的情況下

小心,你的財產是一個簡單的引用您的ImageView的。因此,改變它的值不會取代子視圖樹中的初始值。相反,保持你的imageView,只是設置其UIImage屬性會更好。

yourImageView.image = aNewImage. 
0

插入TargetDetailTableViewCell類中的方法:

- (void)setDotImage:(UIImage*)dot{ 



[self.DotImageView setImage:dot]; 


} 

這對我的作品。我希望它能幫助你!

相關問題