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

Given indexPath for UITableViewCell, load that specific cell

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

我的 UITableViewCell 有一个 class,我在其中设置了一些东西,最重要的是我将 MPMoviePlayer 定位为空 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 是这里的重要功能。仅当 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([=12=], ) })

    //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 中指定的那个。

非常感谢任何帮助。我已经为此苦苦挣扎了好几天。

有一个UITableViewDelegate协议方法

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

实施此方法并在其中检查 indexPath 是否是您想要的,如果是则执行需要完成的工作。

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

应该看起来像

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