当我滚动表格视图时,我的图像会移动到错误的单元格。当图像显示或不显示时,我已经设置了一些条件,但无论我尝试过什么,它们都会继续移动。我的代码如下。
- (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 :(得分:-1)
UITableViewCells
可以重复使用以提高内存效率和性能,因此您需要重置&#34;重置&#34;任何可能根据项目状态发生变化的元素,或者它可能会重新出现在列表中的其他位置。
因此&#34;可重复使用&#34;:
[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// load nib if needed
}
cell.rightImage = nil; // always reset image