2016-07-24 108 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