Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you load video from an array in table detail view when the user clicks on the cell?
    primarykey
    data
    text
    <p>I have loaded a table view with NSArray data. Now, I want to play a video in a detail view. The video played depends on what cell the user clicks. </p> <p>How do I test the cell clicked and load a video from the array based on that? </p> <p>I am using Xcode 5 for iOS 7. </p> <p>Here is my ViewController.m</p> <pre><code>#import "ViewController.h" #import "VideoDetailViewController.h" @interface ViewController () { NSMutableArray * titlearray; NSMutableArray * arrayVidSrc; } @end @implementation ViewController @synthesize mytableview; - (void)viewDidLoad { [super viewDidLoad]; //sets delegate methods of table view self.mytableview.delegate=self; self.mytableview.dataSource=self; //assigns values to objects in array's titlearray = [[NSMutableArray alloc]initWithObjects:@"intro", @"skating",nil]; arrayVidSrc = [[NSMutableArray alloc]initWithObjects:@"Step1-Intro.mp4", @"Step2-Skating.mp4", nil]; } // returns one section in the table -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //counts the items in the title array -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [titlearray count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"vidCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath: indexPath]; cell.textLabel.text = [titlearray objectAtIndex:indexPath.row]; return cell; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"showDetailsSeg"]) { NSIndexPath *indexpath =nil; NSString *titlestring =nil; indexpath = [mytableview indexPathForSelectedRow]; titlestring = [titlearray objectAtIndex:indexpath.row]; NSURL * movieURL = [arrayVidSrc objectAtIndex:indexpath.row]; MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL]; [[player view] setFrame: [self.view bounds]]; [self.view addSubview: [player view]]; [player play]; [[segue destinationViewController] setTitlecontents:titlestring]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </code></pre> <p>And here is my ViewController.h</p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #import &lt;MediaPlayer/MediaPlayer.h&gt; //allows us to use the delegate methods of table view @interface ViewController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate&gt; @property (weak, nonatomic) IBOutlet UITableView *mytableview; @end </code></pre> <p>I have a custom view controller class VideoDetailViewController.m:</p> <pre><code>#import "VideoDetailViewController.h" @interface VideoDetailViewController () @end @implementation VideoDetailViewController @synthesize arrayVidSrc = _arrayVidSrc; @synthesize titlelabel; @synthesize navBar; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.titlelabel.text = self.titlecontents; self.navBar.title = self.titlecontents; //video load from array // NSURL * movieURL = [_arrayVidSrc objectAtIndex:NSIndexPath.row]; //Play the movie now /*MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]; [self presentMoviePlayerViewControllerAnimated:playercontroller]; playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile; [playercontroller.moviePlayer play]; playercontroller = nil;*/ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end </code></pre> <p>and the .h file that goes with it: </p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #import &lt;MediaPlayer/MediaPlayer.h&gt; @interface VideoDetailViewController : UIViewController @property (weak, nonatomic) IBOutlet UINavigationItem *navBar; @property (weak, nonatomic) IBOutlet UILabel *selectedRow; @property (strong, nonatomic) NSString * titlecontents; @property (weak, nonatomic) IBOutlet UILabel * titlelabel; @property (strong, nonatomic) NSArray *arrayVidSrc; @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload