【发布时间】:2016-04-06 15:08:00
【问题描述】:
我怎样才能只播放可见单元格的视频? 一旦单元格不可见就停止视频?
我知道这与 indexPathsForVisibleRows 有关,但在确定如何实现它时遇到问题
当前一次可见单元格。
.......
import UIKit
import AVKit
import AVFoundation
class videoDevTable: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! VideoTableViewCell
let videoURL = NSURL(string: "https://scontent.cdninstagram.com/t50.2886-16/12951809_1538642766431783_2068695559_n.mp4")
let player = AVPlayer(URL: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.playerView.bounds
cell.playerView.layer.addSublayer(playerLayer)
player.play()
return cell
}
【问题讨论】:
-
你的权利。我需要在单元格不显示后清除单元格,并且只在视图中播放单元格
标签: ios swift uitableview avplayer