Search code examples
iosuitableviewswiftmpmovieplayercontroller

Given indexPath for UITableViewCell, load that specific cell


If I know which cell I need to load (from an indexPath), how do I perform an action for only that cell?

I have a class for my UITableViewCell where I set up a few things, most importantly I position an MPMoviePlayer with an empty URL.

class TableViewCell: UITableViewCell {

@IBOutlet weak var titleLabel:UILabel!
@IBOutlet weak var movieView:UIView! //Set up in storyboard
var moviePlayer:MPMoviePlayerController!
var videoURL:NSURL!

override func awakeFromNib() {
    super.awakeFromNib()

    //initialize movie player
    moviePlayer = MPMoviePlayerController(contentURL: videoURL)

}

override func layoutSubviews() {
    //layout movieplayer
    moviePlayer.view.frame = movieView.bounds
    moviePlayer.view.center = CGPointMake(CGRectGetMidX(movieView.bounds), CGRectGetMidY(movieView.bounds))
    movieView.addSubview(moviePlayer.view)
}

//Action to load video
func displayVideo() {
    println("Should display Video at specified indexPath")
    moviePlayer = MPMoviePlayerController(contentURL: videoURL)
    moviePlayer.movieSourceType = MPMovieSourceType.File
    moviePlayer.repeatMode = MPMovieRepeatMode.One
    moviePlayer.controlStyle = MPMovieControlStyle.None
    moviePlayer.prepareToPlay()
    moviePlayer.play()
}

}

displayVideo is the vital function here. It needs to load ONLY when the tableViewCell is taking up a majority of the view. Therefore, I can't call it in cellForRowAtIndexPath.

All I do in cellForRowAtIndexPath is load a label into each cell and set a height variable for adjusting the heightForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let myCell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPath) as TableViewCell

    //Get height of movieView so we can adjust height of row
    if didSetRowHeight == false {
        movieViewHeight = myCell.movieView.frame.height
        didSetRowHeight = true
    }

    //Set label in each cell to the right college
    myCell.titleLabel.text = titleLabels[indexPath.row]

    //This does NOT WORK; loads movies that are not taking majority of view
    //myCell.videoURL = NSURL(string: videoFiles[indexPath.row].url)

    return myCell
}

Next, I determine the indexPath for the cell that is in the majority of the view when scrolling stops. This value is held in indexPathToLoad

override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    //Array to hold distance of visible cells to top of screen
    var distancesToTop = [CGFloat]()
    //Clean out array from previous scroll
    distancesToTop.removeAll(keepCapacity: true)

    //Array of visible cell indexPaths
    var indexPaths = tableView.indexPathsForVisibleRows()!

    for visibleCell in tableView.visibleCells() { //for each visible cell...

        //Append the distance to top of screen
        distancesToTop.append(abs((visibleCell.frame.minY - tableView.contentOffset.y) - 64))

    }

    //Find the lowest distance to top
    let numMin = distancesToTop.reduce(CGFloat.max, { min($0, $1) })

    //Determine the objectForIndexPath that the minimum number was in
    let num = find(distancesToTop, numMin)!

    //Use that to determine the indexPathToLoad from the array of indexPaths
    indexPathToLoad = indexPaths[num]

    //This successfully prints the indexPath that I need to load a movie
    println("indexPath to load: \(indexPathToLoad.row)")

    //Here's where it gets funky:
    //Attempt to access cell from this function so we can load the video at the proper indexPath
    var cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPathToLoad as NSIndexPath) as TableViewCell

    //Load the proper video...
    cell.videoURL = NSURL(string: videoFiles[indexPathToLoad.row].url)

    cell.displayVideo()

}

So I know precisely which tableViewCell that displayVideo() needs to be applied to, but it seems to choose a totally random indexPath, rather than the one specified in indexPathToLoad.

Any help is GREATLY APPRECIATED. I have been struggling with this for days.


Solution

  • The line

    var cell:TableViewCell = tableView.dequeueReusableCellWithIdentifier("tableViewCell", forIndexPath: indexPathToLoad as NSIndexPath) as TableViewCell
    

    should look something like

    if let cell = tableView.cellForRowAtIndexPath(indexPathToLoad) as? TableViewCell {
        cell.displayVideo()
    }