2016-08-30 5 views
0

У меня возникла новая проблема в ожидании решения. Сейчас у меня немного проводная компоновка. Мой MainView - TableView, в одной из 6 пользовательских ячеек есть одна ячейка, в которой есть TableView. Нажимая на ячейку этого внутреннего tableView, я хочу, чтобы segue был новым YTPlayerViewController, представлением для просмотра видео с YouTube с использованием классов Googles YThelper.prepareForSegue из пользовательского UITableViewCell

Проблема в том, что вещи performSegueWithIdentifier и override func prepareForSegue не работают, потому что я должен обрабатывать их в своем обычном классе UITableViewCell, который не знает этих функций.

Есть ли альтернатива?

Я просто хочу передать свое видеоизображение из ячейки в YTPlayerViewController, открыть его и воспроизвести видео. Оттуда должно быть возможно вернуться назад.

надеюсь, что вы, ребята, можете понять и помочь мне.

Привет из германии

Мой класс:

class DetailVideoCell: UITableViewCell , UITableViewDelegate, UITableViewDataSource { 


    @IBOutlet weak var tableView: UITableView! 

    var selectedVideoIndex: Int! 



    /*override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "showPlayer" { 
      let playerViewController = segue.destinationViewController as! YTPlayerViewController 
      playerViewController.videoID = Data.youTubeVideos[selectedVideoIndex]["videoId"] as! String 
     } 
    }*/ 



    // MARK: TableViewDataSource functions. 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return Data.youTubeVideos.count ?? 3 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: VideoDetailCell = tableView.dequeueReusableCellWithIdentifier("VideoDetailCell") as! VideoDetailCell 

     let url = NSURL(string: Data.youTubeVideos[indexPath.row]["thumbnail"] as! String) 
     let data = NSData(contentsOfURL: url!) 
     let image = UIImage(data: data!) 

     cell.videoThumbnail.image = image 
     cell.channelTitle.text = Data.youTubeVideos[indexPath.row]["channelTitle"] as? String 
     cell.videoTitle.text = Data.youTubeVideos[indexPath.row]["title"] as? String 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     selectedVideoIndex = indexPath.row 
     print(selectedVideoIndex) 
     //performSegueWithIdentifier("showPlayer", sender: indexPath) 
    } 

} 
+1

почему этот материал не работает? Вы должны передать их в свой класс контроллера tableView, а не в подклассу tablecell. –

+0

Да, но мой контроллер таблиц - это мой tableviewcell. – kuemme01

+0

Вы реализуете 'UITableViewDelegate' где угодно? – frankfg

ответ

0

В вашем DetailVideoCell:

// Create a protocol that will act as the delegate 

protocol DetailVideoCellDelegate: class { 
    func selectedCellAtIndex(index: Int) 
} 

class DetailVideoCell: UITableViewCell , UITableViewDelegate, UITableViewDataSource { 


@IBOutlet weak var tableView: UITableView! 

var selectedVideoIndex: Int! 

// Create a delegate object 
weak var delegate: DetailVideoCellDelegate? 


/*override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
if segue.identifier == "showPlayer" { 
let playerViewController = segue.destinationViewController as! YTPlayerViewController 
playerViewController.videoID = Data.youTubeVideos[selectedVideoIndex]["videoId"] as! String 
} 
}*/ 



// MARK: TableViewDataSource functions. 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return Data.youTubeVideos.count ?? 3 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: VideoDetailCell = tableView.dequeueReusableCellWithIdentifier("VideoDetailCell") as! VideoDetailCell 

    let url = NSURL(string: Data.youTubeVideos[indexPath.row]["thumbnail"] as! String) 
    let data = NSData(contentsOfURL: url!) 
    let image = UIImage(data: data!) 

    cell.videoThumbnail.image = image 
    cell.channelTitle.text = Data.youTubeVideos[indexPath.row]["channelTitle"] as? String 
    cell.videoTitle.text = Data.youTubeVideos[indexPath.row]["title"] as? String 

    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    selectedVideoIndex = indexPath.row 
    print(selectedVideoIndex) 

    // use the delegate to send the selected index back to your MainView 
    delegate?.selectedCellAtIndex(selectedVideoIndex) 
    //performSegueWithIdentifier("showPlayer", sender: indexPath) 
    } 

} 

В вашем MainView:

class MainView: DetailVideoCellDelegate { 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    // However you are configuring your cells set the DetailVideoCell delegate to self 

    if cell.delegate == nil { 
     cell.delegate = self 
    } 

    return cell 
} 

func selectedCellAtIndex(index: Int) { 

    // Or whatever you are using the index for 
    let identifier = yourArray[index] 

    // Easiest way is to use a navigation controller to handle the segues to be able to go back 
    // Otherwise you will need to do it in a custom way 
    self.navigationController?.performSegueWithIdentifier(identifier, sender: self) 

    } 
} 
+0

Да! Спасибо, мужик. В сочетании с той ссылкой, которую вы сначала отправили, я решил ее. Ваш код был прав, но вы должны написать func selectedCellAtIndex внутри расширения: «extention MainView: DetailVideoCellDelegate {func selectedCellAtIndex (index: Int) {...}}. Спасибо! – kuemme01

+0

Нет проблем. Также да, вы правы, что вы можете поместить его в расширение, но это не обязательно. –

+0

Без cell.delegate = self не разрешено компилятором – kuemme01

Смежные вопросы