2014-12-06 88 views
0

我有一個tableview不同section,每個部分只有一行。如果有任何部分被選中,則在MPMoviePlayerviewcontroller中播放視頻。點擊Done按鈕後,我的應用只會崩潰一些時間(某些部分僅限於特定情況)。原因:'UITableView dataSource必須從tableView返回一個單元格:cellForRowAtIndexPath

我的代碼如下:

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]; 

我已經查了幾個其他的問題,但仍然想不通爲什麼發生這種情況。

請告訴任何一個....

+0

plz add view does load and did appear code。 – 2014-12-06 07:44:34

+0

檢查您是否在cellForRow中返回非null值 – Alexi 2014-12-06 07:46:33

回答

0

您可以嘗試使用,而不是在你的moviePlayBackDidFinish使用removeFromParentViewControllerdismissMoviePlayerViewControllerAnimated方法。

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