I'm working on a netflix style app for tvOS and I've followed some tutorials and I've done my share of stackOverflow research. Which has been very helpful, it's actually how I found the delegate solution I'm trying to implement. With the problem I'm having now, most of what I've found on here reference different popup views or containers. That's not exactly what my issue is so I'm having a hard time. I'm also pretty much brand spanking new iOS, tvOS, Swift in general, most of my previous dev work was JS, AS3, Classic ASP, PHP...
To the point: I have a view structure as shown:view structure
I have a class for my main ViewController, called ListViewController, I have a class for my table view cell called ListTableViewCell, the reference is "rowCell" as you see in the image. I have a calls for my collection view cell called ListViewCell.
My ListTableViewCell is structured like this :
import UIKit
protocol ListViewDelegate {
func updatePreview (name: String,runtTime:Int,description:String,level:String)
}
class ListTableViewCell: UITableViewCell{
var delegate:ListViewDelegate?
var list:List?
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension ListTableViewCell : UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (self.list?.MediaObjects.count != nil){
return (self.list?.MediaObjects.count)!
}else{
return 0
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! ListViewCell
let list = self.list!
let media = list.MediaObjects
cell.mediaLabel.text = media[indexPath.row].name
return cell
}
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
}
override func shouldUpdateFocusInContext(context: UIFocusUpdateContext) -> Bool {
let list = self.list!
let cell: UICollectionViewCell = context.nextFocusedView as! UICollectionViewCell
let indexPath: NSIndexPath? = self.collectionView.indexPathForCell(cell)
if let path = indexPath{
let name = list.MediaObjects[path.row].name
let runTime = list.MediaObjects[path.row].runTime
let description = list.MediaObjects[path.row].description
let level = list.MediaObjects[path.row].level
print(path.row)
if let del = delegate {
del.updatePreview(name,runtTime:runTime,description:description,level:level)
}
}
return true
}
func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let playerVC = PlayerViewController()
playerVC.videoFile = (list?.MediaObjects[indexPath.row].fileURI)!
playerVC.playVideo()
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(playerVC, animated: true, completion: nil)
}
}
The delegate is always nil.
here is the code for my UIViewController class -
import UIKit
class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, ListViewDelegate {
@IBOutlet weak var tableView: UITableView!
var lists = [List]()
@IBOutlet weak var videoDescriptionLabel: UILabel!
@IBOutlet weak var videoLevelLabel: UILabel!
@IBOutlet weak var videoRunTimeLabel: UILabel!
@IBOutlet weak var videoTitleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let listLoader = loadLists()
listLoader.loadData { (dataLoaded:Bool, lists:[List]) in
if (dataLoaded == true){
print("complete (self.lists.count)")
self.lists = lists
self.tableView.reloadData()
}else {
print("data load error")
}
}
self.tableView.dataSource = self
self.tableView.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.lists.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let row = self.lists[section]
return "(row.name)"
}
func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("rowCell") as! ListTableViewCell
let list = self.lists[indexPath.row]
cell.list = list
return cell
}
func updatePreview(name: String,runtTime:Int,description:String,level:String) {
print("delegage method called")
self.videoLevelLabel.text = level
self.videoTitleLabel.text = name
self.videoDescriptionLabel.text = description
self.videoRunTimeLabel.text = String(runtTime)
}
}
This code is nowhere near production ready, I know that, no need to comment on how I'm handling optionals, etc... I'm already fully aware there other issues I need to clean up. Any feedback on code structure or better approaches or just "you're doing this all wrong" would be greatly appreciated but please keep comments on topic. My code works, I pull data in from my own Parse Server instance, I can navigate through the playlists and videos and programmatically call up the player and they will play. The only thing I'm having problems with is getting data to bubble back up to the VC so I can update the appropriate Outlets.
The app I am building will never be a production app, it's a proof of concept and a learning exercise. I generally don't ask questions on here because I'm pretty good at using the site to find answers I need but I'm stumped on this one.
Aucun commentaire:
Enregistrer un commentaire