2016-07-24 104 views
0

當我滾動表格視圖時,我的圖像移動到錯誤的細胞。當圖像應該顯示或不顯示時,我已經設定了一些條件,但無論我嘗試了什麼,他們只是繼續移動。我的代碼如下。Tableview細胞圖像滾動時移動到不同的位置

- (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; 

} 

播放按鈕和曲目標籤很好,它們按照它們應該的方式工作,但右圖像是問題所在。

前5個單元在第一次加載時不應該有圖像,在第二次加載時,前5個單元應該有星形圖像,其餘單元應該有掛鎖圖像。在IAP完成之後,根據indexpath.row,那個掛鎖圖像應該不再存在。

我正在檢查與NSUserDefaults。

我希望我解釋得很好。

感謝所有幫助

+0

你能否解釋一下什麼叫「先裝」和「IAP已經取得了」是什麼意思? – ArG

+0

雖然我會需要更多的細節,但我的直覺是你需要在你的CustomTableView(cell)中進行清理。 覆蓋: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