重用的单元格具有来自先前单元格的UIView

Bro*_*sef 3 cocoa-touch objective-c uitableview ios

我有一个tableview在我看来,控制器和每个tableview cell与音频文件相关联.我正在使用具有UIView内部的自定义tableview单元格.每个单元格的UIView都有一个UIBezierPath用蓝色描边的唯一.当细胞被挖掘,didSelectRowForIndexPath开始播放音频文件和蓝色UIBezierPath被抚摸在一个红色的UIBezierPath相对于音频文件的进度.

float progress =  playTime/duration;
self.redWave.strokeEnd = self.progress;
Run Code Online (Sandbox Code Playgroud)

这工作得很好.我注意到的是,当我点击第一个单元格时,音频将开始播放,红色路径将开始被描边 - 这可能会发生,但如果我在发生这种情况时向下滚动,则第五个单元格从我点击了同样的UIBezierPath,它也在蓝色路径上划出红色路径!很明显,细胞正在被重复使用.此外,当我点击一个单元格时,这不仅仅发生了 - 当我的tableview加载时,默认情况下,前5个单元格中的UIBezierPath会复制到下一组5个单元格中.我不知道怎么解决这个问题.有任何想法吗?

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

    static NSString* identifier = @"audioTableCell";

    OSAudioTableCell *cell = [self.audioTable dequeueReusableCellWithIdentifier:identifier];

    if(cell == nil)
    {
         cell = [[OSAudioTableCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier];

    }

        //Pull data from NSFetchResultsController
        Recording * recording = [self.managedDocument.frc objectAtIndexPath:indexPath];

        cell.waveView.minAmpl = [recording.minAmpl floatValue];

        //UIBezierPath Data
        NSData * pathData = recording.waveData;

        //set path for UIView (waveView is custom UIView class)
        cell.waveView.path = [NSKeyedUnarchiver unarchiveObjectWithData:pathData];
        [cell setImageSize:cell.waveView.bounds.size];

        cell.tag = indexPath.row;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //tableCell textfield 
        NSString * track = @"Track";
        NSInteger row = indexPath.row;
        row = [self.managedDocument.frc.fetchedObjects count] - row;
        track = [track stringByAppendingFormat:@" %li",(long)row];
        cell.trackLabel.text = track;
        [cell.textField setFont:[UIFont fontWithName:@"Raleway-SemiBold" size:12]];
        cell.textField.delegate = self;

        if([recording.trackTitle isEqualToString:@""])
        {
            cell.textField.text = track;
        }
        else
        {
            cell.textField.text = recording.trackTitle;

        }

        UIColor * circleColor = [UIColor colorWithRed:107/255.0f green:212/255.0f blue:231/255.0f alpha:1.0f];

        cell.trackIcon.backgroundColor = circleColor;

        [cell.trackIcon.layer setCornerRadius:cell.trackIcon.bounds.size.width/2];

        cell.trackIcon.layer.masksToBounds = YES;


return cell;
Run Code Online (Sandbox Code Playgroud)

}

didSelectRowAtIndexPath在我的tableViewCell类中调用以下方法:

-(void) rowSelected2: (NSURL*) url
{

    self.audioPlayer = [OSTablePlayerController getInstance];
    NSData *data=[NSData dataWithContentsOfURL:url];
    [self.audioPlayer playAudio:data];
    self.audioPlayer.isPlayerPlaying = YES;
    self.timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

 }
Run Code Online (Sandbox Code Playgroud)

mar*_*ega 6

表视图旨在重用行.这是一项可以提高应用性能的功能,尤其是当您的表视图中包含大量元素时.

由于表视图重用了它们的行,因此您需要在回收行之前手动清除它们.如果您通过创建UITableViewCell子类来实现单元格,则可以覆盖它

- (void)prepareForReuse

方法.每次要重用一个单元时都会调用它,这是进行任何可能需要进行的清理的最佳位置.