2015-01-13 5 views
7

私はUITableViewCellにsdwebimageでダウンロードした画像を設定しています。 UITableViewに間違った画像が表示されないようにするには、nilの画像をprepareForReuseに設定する必要があります。しかし、私はこれを実装するいくつかの問題を抱えています。私は画像設定場所です`prepareForReuse`はどのように実装しますか?

:私は- (void)prepareForSegue Xcodeのを実装する場合

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * reuseIdentifier = @"programmaticCell"; 
    MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 
    if (!cell) { 
     cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 
    } 

    CGFloat brightness = [UIScreen mainScreen].brightness; 

    cell.textLabel.text = [self.streams[indexPath.row] name]; 
    cell.detailTextLabel.text = [self.streams[indexPath.row] published]; 

    NSString *imageUrl = [NSString stringWithFormat: @"%@", [self.streams[indexPath.row] photo]]; 

    NSLog(@"Image is: %@ and path is: %d", imageUrl, indexPath.row); 

    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] 
         placeholderImage:[UIImage imageNamed:@"tile-blue.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; 

    cell.delegate = self; //optional 

    return cell; 
} 

cellがそれに利用できないというエラーを投げ続けます。これを実装する適切な方法は何ですか?あなたのMGSwipeTableCell.mにこれを追加すること

+1

'prepareForReuse'があなたの' MGSwipeTableCell'に実装されています。ここでは、セルへの参照を 'cell'ではなく' self'と呼びます。 –

+0

'self.textLabel = nil'を実行すると' Property 'textLabel'が 'ViewController'型のオブジェクトに見つかりません。' – user4334509

+1

'prepareForReuse'の実装として' ViewController'に 'prepareForSegue'と間違っていますonあなたの 'MGSwipeTableCell'。 '@implementation MGSwipeTableCell'に行き、そこで' prepareForReuse'を実装してください。 –

答えて

10

試してみてください。

-(void)prepareForReuse 
{ 
    [super prepareForReuse]; 

    self.textLabel.text = @""; 
    self.detailTextLabel.text = @""; 
    self.imageView.image = nil; 
} 
+7

Mikeが知っている可能性のある上記のソリューション(デモコード)にコメントしてください:セルのコンテンツをリセットするためにprepareForReuseを実装しないでください。外観をリセットするだけです。ドキュメントから: "パフォーマンス上の理由から、アルファ、編集、選択状態など、コンテンツに関連しないセルの属性のみをリセットする必要があります。"さらに、「tableViewのdelegate:cellForRowAtIndexPath:セルを再利用する際に常にすべてのコンテンツをリセットする必要があります。 cellForRowAtIndexPathにテキストのようなコンテンツを設定し、 '' 'PrepForReuse'''で外観をリセットします。 –

+0

その説明をありがとう。私は実際にそれを知らなかった。私はそれを将来も心に留めておきます。通常、私はcellForRowAtIndexPathにコンテンツを設定しますが、prepareForReuseでコンテンツをリセットすることがあります。 –

+3

大歓迎です!私はずっと前にこれを見つけました。 UITableViewから最高のパフォーマンスを引き出そうとするときは、心に留めておくとよいでしょう。 –

関連する問題