2010-11-02 71 views
1

我想打開一個視頻,當我點擊第一個單元格時,我想在第二個單元格點擊時打開另一個視頻,等等。如何在點擊單元格時播放視頻?

請問有人能告訴我我該怎麼做?我有這個代碼。

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return 3;//[array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    // create a cell 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleSubtitle 
       reuseIdentifier:@"cell"]; 
    } 

    //retrieve an image/ Zet een afbeelding naast de titel 
    NSString *imagefile = [[NSBundle mainBundle] 
          pathForResource:@"cellimage" ofType:@"png"]; 
    UIImage *ui = [[UIImage alloc] initWithContentsOfFile:imagefile]; 
    //set the image on the table cell/ zet hem in de tabel 
    cell.imageView.image = ui; 


    //set the main text/hoofd tekst 
    cell.textLabel.text = [exercises objectAtIndex:indexPath.row]; 

    //set the subtitle text/ ondertekst 
    cell.detailTextLabel.text = @"Management training", @"Ondertiteling hier", @"Ondertiteling hier"; 



    //accessory type /zijn de navigatie pijltjes, er zijn 3 verschillende 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

    //return the cell 
    //cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 
} 

/* 
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    exercises = [[NSArray alloc] 
       initWithObjects:@"BAZO", @"Ontslag gesprek", 
       @"Management training",nil]; 

    self.title = @"Training lijst"; 

    //Probeerzel 
    /*array = [[NSMutableArray alloc] init]; 
    [array addObject:@"Bazo"]; 
    [array addObject:@"secondmovie"];*/ 


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

我知道它的很多代碼,但這是表的代碼。

我已經導入了MediaPlayer.framework。但我不知道如何將一個單元格鏈接到視頻。

謝謝你的時間!

布拉姆

回答

2

你還需要設置你的視圖控制器爲您的表視圖的委託,並實現 - 在您的視圖控制器的委託方法[的UITableViewDelegate的tableView:didSelectRowAtIndexPath方法。當用戶點擊一個表格單元格時,這將被調用。

在你執行的tableView的:didSelectRowAtIndexPath方法:,你將建立一個對的MPMoviePlayerController播放電影,這樣的事情:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // First, get the URL for the video file you want to play. For example, if you have an array of the movie file URLs, you'd do this: 
    NSURL * movieURL = [_movieFileURLs objectAtIndex:indexPath.row]; 

    // Now set up the movie player controller and play the movie. 
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL]; 
    [[player view] setFrame: [self.view bounds]]; // frame must match parent view 
    [self.view addSubview: [player view]]; 
    [player play]; 
} 

您還可能希望在http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html%23//apple_ref/doc/uid/TP40006953

檢查出來的MPMoviePlayerController的文檔