给定UITableViewCell的indexPath,加载该特定单元格

时间:2015-03-05 17:09:57

标签: ios uitableview swift mpmovieplayercontroller

如果我知道需要加载哪个单元格(来自indexPath),我该如何仅对该单元格执行操作?

我有一个UITableViewCell类,我设置了一些东西,最重要的是我用一个空URL定位MPMoviePlayer。

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是这里至关重要的功能。它只需要在tableViewCell占用大部分视图时加载。因此,我无法在cellForRowAtIndexPath中调用它。

我在cellForRowAtIndexPath中所做的就是在每个单元格中加载一个标签,并设置一个高度变量来调整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
}

接下来,我确定滚动停止时大多数视图中的单元格的indexPath。该值保存在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()

}

所以我确切地知道displayVideo()需要应用于哪个tableViewCell,但它似乎选择了一个完全随机的indexPath,而不是indexPathToLoad中指定的那个。

任何帮助都非常令人满意。几天来我一直在努力。

2 个答案:

答案 0 :(得分:2)

该行

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

应该看起来像

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

答案 1 :(得分:0)

UITableViewDelegate协议方法

optional func tableView(_ tableView: UITableView,
        willDisplayCell cell: UITableViewCell,
      forRowAtIndexPath indexPath: NSIndexPath)

实现此方法并在其中检查indexPath是否是您想要的,以及是否需要完成的工作。