【发布时间】:2016-12-31 21:19:25
【问题描述】:
我正在尝试用要播放的视频填充我的 VideoFeed。但是每当我上传新视频时,Feed 上的每个视频都会与上一个视频相同。
谁能帮我找出问题所在?使用 Swift 3。
import UIKit
import Firebase
import AVKit
import AVFoundation
class VideoFeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var tableView: UITableView!
var post: Post!
var posts = [Post]()
var imagePicker : UIImagePickerController! //Sets imagePicker to type UIImagePickerController
var url: URL! //URL for video
var destination: AVPlayerViewController! //Destination for video
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
imagePicker = UIImagePickerController()
DataService.ds.REF_VIDEOS.observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print("SNAP: \(snap)") //Prints all snapshots
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let id = snap.key
let post = Post(postID: id, postData: postDict)
self.posts.append(post)
}
}
}
self.tableView.reloadData() //Need this
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//return UITableViewCell()
post = posts[indexPath.row]
print(post.videoURL)
if let cell = tableView.dequeueReusableCell(withIdentifier: "videoCell") as? VideoCell {
cell.configureCell(post: post)
return cell
} else {
return VideoCell()
}
}
// This function gets the video to play.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "videoToPlay" {
let destination = segue.destination as! AVPlayerViewController
url = URL(string: post.videoURL)
if let movieURL = url {
destination.player = AVPlayer(url: movieURL)
}
}
}
}
【问题讨论】:
标签: swift uitableview video firebase firebase-realtime-database