在Swift中,xcode虽然正确定义但无法调用委托函数。 用它来获取活动单元格的值。
按钮声明文件
import UIKit
protocol CellDelegate : class {
func didClickPlayButton(_ sender: VideoTableViewCell)
}
class VideoTableViewCell: UITableViewCell {
@IBOutlet weak var cellView: UIView!
@IBOutlet weak var VideoThumbnail : UIImageView!
@IBOutlet weak var VideoName: UILabel!
@IBOutlet weak var uploadDate: UILabel!
@IBOutlet weak var playButton: UIButton!
weak var Delegate: CellDelegate?
@IBAction func pressedPlay(_ sender: UIButton) {
Delegate?.didClickPlayButton(self)
print("PLAY")
}
}
功能声明文件
导入UIKit
导入FirebaseAuth
导入FirebaseDatabase
导入AVFoundation
导入AVKit
类VideoVC:UIViewController,UITableViewDelegate,UITableViewDataSource,CellDelegate {
let avPlayerViewController = AVPlayerViewController()
var avPlayer : AVPlayer?
func didClickPlayButton(_ sender : VideoTableViewCell) {
let indexPath = self.tableview.indexPath(for: sender)
print("Play Button Pressed")
if let index = indexPath?.row
{
let movieUrl : NSURL? = NSURL(string : posts[(index)].noticeURL)
if let url = movieUrl
{
self.avPlayer = AVPlayer(url: url as URL)
self.avPlayerViewController.player = self.avPlayer
self.present(self.avPlayerViewController, animated: true, completion: {
self.avPlayerViewController.player?.play()
})
}
}
}
@IBOutlet weak var tableview: UITableView!
var posts = [Post]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Videocell") as! VideoTableViewCell
cell.selectionStyle = .none
cell.uploadDate?.text = posts[indexPath.row].uploadDate
cell.VideoName?.text = posts[indexPath.row].noticeName
cell.VideoThumbnail?.image = thumbnail(sourceURL: (URL(string : posts[indexPath.row].noticeURL)!))
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableview.dataSource = self
self.tableview.rowHeight = 300
loadposts()
}
func loadposts()
{
Database.database().reference().child("Videos").observe(.childAdded) { (snapshot : DataSnapshot) in
if let dict = snapshot.value as? [String : Any]
{
let uploaddate = dict["Date"] as! String
let noticename = dict["Name"] as! String
let ImageUrl = dict["URL"] as! String
let post = Post(uploadedDate: uploaddate, nameofnotice: noticename, URLofnotice: ImageUrl)
self.posts.append(post)
self.tableview.reloadData()
}
}
}
func thumbnail(sourceURL:URL) -> UIImage {
let asset = AVAsset(url: sourceURL)
let imageGenerator = AVAssetImageGenerator(asset: asset)
let time = CMTime(seconds: 1, preferredTimescale: 1)
let imageRef = try? imageGenerator.copyCGImage(at: time, actualTime: nil)
if imageRef != nil
{
let uiimage = UIImage(cgImage: imageRef!)
return uiimage
}
else
{
return #imageLiteral(resourceName: "BG_DDIT")
}
}
}
答案 0 :(得分:1)
在cellForRow
cell.Delegate = self
并在PdfVC
func didClickPlayButton(_ sender: VideoTableViewCell){----}
答案 1 :(得分:-1)
将您的ViewController设为您的单元格的委托
在ViewController中使用此单元格的cellForRow_atIndexpath中添加这些行
git filter-branch
另外,如果您使用的是swift 3或更高版本,请确保在您的单元格类中以正确的方式制作了协议: -
cell.delegate = self
并且不要让代表变弱。只需将其声明为
protocol YourDelegate {
func yourFunction()
}