2016-08-30 51 views
0

我有一個新的問題等待解決方案。我現在有一點佈線佈局。我的MainView是一個TableView,6個自定義單元格之一是一個單元格,它有一個TableView。通過點擊這個內部tableView的單元格,我想要一個新的YTPlayerViewController,一個使用Google YThelper類觀看YouTube視頻的視圖。prepareForSegue出自定義UITableViewCell

問題是,performSegueWithIdentifieroverride func prepareForSegue東西不起作用,因爲我必須在我不知道這些函數的自定義UITableViewCell類中處理它們。

有沒有其他的選擇?

我只是想將我的videoId從單元傳輸到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

是的,但我的tableview控制器是我的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