2012-02-02 14 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

+0

でそれをリンクすることを忘れていた: "私は私のUITableViewSubclassでDotImageViewのプロパティを持って"それはUITableViewCellのですか?私は、cellForRowAtIndexPathメソッドであなたのUITableViewCell宣言を見る必要があると思います。 – TheRonin

+0

@エルネスト私はペン先をチェックして接続しています。 – Crystal

答えて

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; 

ようにコードを変更する

あなたcell.DotImageViewがないXIB

上で、そのオブジェクトに、この地域のイメージビューオブジェクトを指している今 cell.DotImageViewに新しいUIImageViewオブジェクトを割り当てるです
+0

彼はView Controller上のTargetCellというコンセントにセルをバインドして、そのプロパティにセルをロードしています。これは有効なアプローチであり、objectAtIndexバージョンよりも型安全です(セルがnibの最初のオブジェクトではない場合はどうでしょうか?)しかし、私は同意しています。ほとんどの場合。私はそれがなぜ彼のコードが動作しないのだとは思わない。 –

+0

again again .... –

0

私はそれは私がもともと持っていた他の方法対作品、なぜ私はわからないんだけど、それは

[cell.DotImageView setImage:dot]; 

と協力しました。

+0

セルのimageViewを置き換えるときに、新しいimageViewをセルのサブビューとして追加する必要があります。そうでなければ、セルのプロパティですビュー階層には実際にはありません。正直言って、私は後者のアプローチに固執します - はるかに簡単です。 –

0

1 - 基本ケース

単に標準のUITableViewCellの画像プロパティを使用します。ほとんどの場合に適しています。

2 - あなたのカスタムケース

注意してください、あなたの財産は、あなたのImageViewのに単純な参照です。したがって、その値を変更しても、サブビューツリーの最初の値は置き換えられません。代わりに、あなたのimageViewを保持し、そのUIImageプロパティを設定するだけで良いでしょう。 TargetDetailTableViewCellクラスに

yourImageView.image = aNewImage. 
0

挿入方法は:

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



[self.DotImageView setImage:dot]; 


} 

それは私のために動作します。それがあなたを助けることを願っています!

関連する問題