2016-07-24 13 views
0

テーブルビューをスクロールすると、画像が間違ったセルに移動します。画像を表示するか表示しないかを条件に設定していますが、何を試しても、動き続けています。下の私のコード。スクロール時に異なる位置に移動するテーブルビューのセル画像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellID = @"myCell"; 

    CustomTableView *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 


    if (cell == nil) { 
     if (self.view.frame.size.height == 736) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCelliPhone6+" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 

     } 
     else if (self.view.frame.size.height == 667){ 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCelliPhone6" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 
     else if (self.view.frame.size.height == 568) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCelliPhone5" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 

     } 
     else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCelliPad" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 
     else { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableView" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
     } 

    } 

    MusicFileInfo *musicFileInfo = [mMusicInfoArray objectAtIndex:indexPath.row]; 
    cell.trackName.text = musicFileInfo.mDesc; 
    cell.trackName.backgroundColor = [UIColor clearColor]; 
    cell.trackName.font = [UIFont fontWithName:@"AvenirNext-Regular" size:13.0]; 

    if (songPlaying == indexPath.row && mAudioPlayer.isPlaying) { 

     [cell.playButton setBackgroundImage:[UIImage imageNamed:@"pauseBtn.png"] forState:UIControlStateNormal]; 
     [cell.playButton addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.playButton.tag = indexPath.row; 

    } 

    else { 
     [cell.playButton setBackgroundImage:[UIImage imageNamed:@"playBtn.png"] forState:UIControlStateNormal]; 
     [cell.playButton addTarget:self action:@selector(playPreview:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.playButton.tag = indexPath.row; 
    } 



    if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"pack%li", (long)indexPath.row]] == true || [[NSUserDefaults standardUserDefaults] boolForKey:@"unlockAllTracks"] == true) { 

     cell.rightImage.image = nil; 


    } 

    if ([[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"pack%li", (long)indexPath.row]] == false) { 
     if (indexPath.row <= 4) { 
      cell.rightImage = nil; 

     } else 

     [cell.rightImage setImage:[UIImage imageNamed:@"iapBtnStore.png"]]; 

    } 


    cell.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:235.0f/255.0f blue:236.0f/255.0f alpha:1]; 
    cell.tag = indexPath.row; 
    return cell; 

} 

再生ボタンとトラックのラベルは問題なく動作しますが、rightImageは問題のある場所です。

最初の5つのセルには最初の読み込み時にイメージがありません.2番目の読み込みでは、最初の5つのセルにスターイメージがあり、残りのセルには南京錠イメージがあります。 IAPが作成された後、indexpath.rowに応じて、そのパドロックイメージはもう存在しなくなります。

私はそれをNSUserDefaultsで確認しています。

私は正しく説明しました。任意のヘルプ

+0

あなたは「最初のロード」と「IAPが行われた」とはどういう意味ですか手の込んだことはできますか? – ArG

+0

私はそれについてさらに詳細が必要ですが、私の勘違いはあなたのCustomTableView(セル)でクリーンアップを行う必要があるということです。 オーバーライド:UITableViewCellクラスのprepareForReuseメソッドとそれに self.rightImage = nilを設定します。 このメソッドは、セルが再利用(またはリサイクル)されたときに呼び出され、セルのクリーンアップを確実に行うことができます。 – ArG

答えて

-1

UITableViewCellsため

おかげで、メモリ効率とパフォーマンスを向上させるために再利用しますので、あなたは、アイテムの状態に基づいて変更される場合があります任意の要素を「リセット」する必要があるか、それが再表示される場合がありますリストのどこか。したがって、「再利用可能」

[tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) { 
    // load nib if needed 
} 
cell.rightImage = nil; // always reset image 
+0

仲間ありがとう、私はそれを並べ替えている:) – Johnny

関連する問題