原因:"UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回一个单元格



我有一个具有不同section的表格视图,每个部分只有一行。 如果选择了任何部分,则视频正在MPMoviePlayerviewcontroller中播放。单击Done按钮后,我的应用程序仅崩溃了几次(某些部分仅特定)。

我的代码如下:

subcategorytable=[[UITableView alloc]initWithFrame:CGRectMake(0,0,375,667) style:(UITableViewStylePlain)];
subcategorytable.dataSource=self;
subcategorytable.delegate=self;
subcategorytable.separatorColor=[UIColor orangecolour];
subcategorytable.hidden=YES;
subcategorytable.backgroundView=nil;
[subcategorytable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"subcatcellid"];
[self.view addSubview:subcategorytable];
     ***cellForRowAtIndexPath***
    static NSString *cellidentifier3 = @"subcatcellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier3
                             ];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier3];
    }
    cell.textLabel.text=[productarray objectAtIndex:indexPath.section];
    UIFont * font=[UIFont fontWithName:@"TrebuchetMS-Bold" size:18];
    cell.textLabel.font=font;
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.backgroundColor=[UIColor orangeColor];
     cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
  ***didSelectRowAtIndexPath***
    NSString *strurl=[NSString stringWithFormat:@"%@",[producturl objectAtIndex:indexPath.section]];
    strurl=[strurl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSURL * imageURL = [NSURL URLWithString:strurl];
    NSDictionary *videos = [HCYoutubeParser h264videosWithYoutubeURL:imageURL];
    if (!movieplayer)
        movieplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[videos objectForKey:@"medium"]]];
    [movieplayer.moviePlayer setControlStyle: MPMovieControlStyleFullscreen];
    [movieplayer.moviePlayer setFullscreen:YES animated:YES];
    [self presentMoviePlayerViewControllerAnimated:movieplayer];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:movieplayer.moviePlayer];
          ***moviePlayBackDidFinish***
[movieplayer removeFromParentViewController];
movieplayer =  nil;
subcategorytable.hidden=NO;
[subcategorytable reloadData];

我已经检查了其他几个问题,但仍然无法弄清楚为什么会发生这种情况。

请告诉任何人...

您可以

尝试使用dismissMoviePlayerViewControllerAnimated方法,而不是在moviePlayBackDidFinish中使用removeFromParentViewController

-(void)moviePlayBackDidFinish:(NSNotification*)aNotification{
    int value = [[aNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (value == MPMovieFinishReasonUserExited) {
        [self dismissMoviePlayerViewControllerAnimated];
    }
    subcategorytable.hidden=NO;
    [subcategorytable reloadData];
}

最新更新