原因:'UITableView dataSource必须从tableView:cellForRowAtIndexPath返回一个单元格

时间:2014-12-06 07:32:38

标签: ios objective-c iphone uitableview

我有一个不同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];

我已经检查过其他几个问题,但仍然无法弄清楚为什么会这样。

请告诉任何人......

1 个答案:

答案 0 :(得分:0)

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

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