gpt4 book ai didi

ios - 自定义 UITableViewCell 中的prepareForSegue

转载 作者:行者123 更新时间:2023-11-30 13:07:00 25 4
gpt4 key购买 nike

我有一个新问题等待解决。我现在有一点有线布局。我的 MainView 是一个 TableView,在 6 个自定义单元格之一中有一个单元格中有一个 TableView。通过单击此内部 tableView 的单元格,我想要转至新的 YTPlayerViewController,这是一个使用 google YThelper 类观看 YouTube 视频的 View 。

问题是, performSegueWithIdentifieroverride funcprepareForSegue 的东西不起作用,因为我必须在不知道这些函数的自定义 UITableViewCell 类中处理它们.

还有其他选择吗?

我只想将我的 videoId 从单元传输到 YTPlayerViewController,打开它并播放视频。从那里应该可以返回。

希望大家能够理解并帮助我。

来自德国的问候

我的类(class):

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)
}

}

最佳答案

在您的 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)
}

}

在你的主视图中:

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)

}
}

关于ios - 自定义 UITableViewCell 中的prepareForSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com