细胞显示错误内容

时间:2015-09-08 12:03:58

标签: objective-c uitableview cell

我用图像和视频填充单元格。有些人有图片,有些人有视频。

对于图像,没问题。然而,当显示图片和视频时,事情变得奇怪。包含视频的单元格将为空白,但下一个单元格将显示图像从未出现的视频。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object{
static NSString *simpleTableIdentifier = @"cell";

self.cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (self.cell == nil) {
    self.cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


PFFile *videoFile = [object objectForKey:@"video"];
PFFile *imageFile = [object objectForKey:@"photo"];

if (imageFile != nil) {

    self.cell.movie.view.hidden = YES;
    [self.cell.movie stop];        
    self.cell.photo.hidden = NO;
    self.cell.photo.file = imageFile;
    [self.cell.photo loadInBackground];
}

else {
       dispatch_async(dispatch_get_main_queue(), ^(void){                

            [self.cell.movie setContentURL:[NSURL URLWithString:videoFile.url]];
            [self.cell.movie prepareToPlay];
            [self.cell.movie play];
            self.cell.movie.view.hidden = NO;

            self.cell.photo.hidden = YES;
    });

}

在customCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    self.opaque = NO;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.accessoryType = UITableViewCellAccessoryNone;
    self.clipsToBounds = NO;

    self.backgroundColor = [UIColor colorWithRed:140.0f/255.0f green:129.0f/255.0f blue:126.0f/255.0f alpha:1.0f];


    self.photo = [[PFImageView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.width)];
    self.photo.backgroundColor = [UIColor colorWithRed:191.0f/255.0f green:191.0f/255.0f blue:191.0f/255.0f alpha:1.0f];        

    self.movie = [[MPMoviePlayerController alloc] init];
    self.movie.controlStyle = MPMovieControlStyleNone;
    self.movie.shouldAutoplay = NO;
    self.movie.repeatMode = MPMovieRepeatModeOne;
    self.movie.scalingMode = MPMovieScalingModeAspectFit;
    self.movie.movieSourceType = MPMovieSourceTypeStreaming;
    self.movie.view.backgroundColor = [UIColor clearColor];


- (void)layoutSubviews {
[super layoutSubviews];
self.movie.view.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width);}

1 个答案:

答案 0 :(得分:0)

您的问题是您正在使用异步工作的dispatch函数,当单元格出列时,您从URL执行加载,同时向下滚动并到达也尝试使用异步获取某些内容的照片单元格[self.cell.photo loadInBackground]

要解决此问题,您需要在NSOperation之类的异步操作中存储一些引用,当您尝试将单元格出列时,需要取消之前的下载过程。

我可以建议谷歌或创建NSOperation队列来获取视频数据,并使用AFNetworking setImageWithURL:来获取带缓存的图像。