gpt4 book ai didi

ios - 如何避免为 TableView 列表使用一百万个 View Controller ?

转载 作者:可可西里 更新时间:2023-11-01 00:28:39 25 4
gpt4 key购买 nike

这是我想做的一个例子。

您有一个包含不同视频列表的表格 View 。当你点击一个视频时,它会带你到一个详细的 View Controller (顶部的视频,下面的描述)。现在我正在创建一个全新的 View Controller ,添加视频/文本,链接 segue,然后创建另一个 View Controller 并重新开始。与其拥有一百万个 View Controller ,不如仅使用一个 View Controller 并从 swift 文件中输入文本/视频来做到这一点?我是比较新的,所以如果有人能清楚地解释这个过程,我将不胜感激。谢谢!

最佳答案

您只需要一个 VC 即可展示您拥有的众多视频。

假设您有这样一个模型:

struct Video {
let videoLink: URL
let description: String
}

并且您的 TableView Controller 使用名为 videosVideo 数组作为其数据源。

在您的 didSelectRowAt 方法中,您可以获得所选视频并以所选视频作为发送者执行 segue:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let videoSelected = videos[indexPath.row]
performSegue(withIdentifier: "showVideo", sender: videoSelected)
}

现在创建一个 VideoViewController.swift 文件并执行如下操作:

class VideoViewController: UIViewController {
var video: Video!

// write code for this VC to display not a specific video, but "self.video"
// For example, instead of setting the label's text to a hardcoded description, set it to "self.video.description"
}

然后,返回到您的 TableView Controller ,并覆盖 prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? VideoViewController {
vc.video = sender as! Video
}
}

关于ios - 如何避免为 TableView 列表使用一百万个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406647/

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