ios - 通过的标签不出现
问题描述
我正在尝试创建一个表格视图,其中一旦选择了一个表格视图单元格,它将单元格的名称传递给一个新的视图控制器。当我运行它并选择一个 tableview 单元格时,我被带到另一个视图控制器,但组织标签不存在。我该如何解决?
这是我的第一个视图控制器的代码。
import UIKit
struct Organizations {
var sectionTitle = String()
var rowTitles = [String]()
}
class SearchOrganizationsViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var searchOrganizations: Organizations?
var organizations = [
Organizations(sectionTitle: "section 1", rowTitles: ["organization 1", "organization 2", "organization 3"]),
Organizations(sectionTitle: "section 2", rowTitles: ["organization 1", "organization 2"]),
Organizations(sectionTitle: "section 3", rowTitles: ["organization 1"]),
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
extension SearchOrganizationsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
cell?.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell?.textLabel?.numberOfLines = 3
if searching {
cell?.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
} else {
cell?.textLabel?.text = self.organizations[indexPath.section].rowTitles[indexPath.row]
}
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedOrganizations = organizations[indexPath.row]
performSegue(withIdentifier: "organizationDetailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? OrganizationsDetailViewController {
destination.organization = selectedOrganizations
}
}
}
}
这是我的第二个视图控制器的代码。
import UIKit
class OrganizationsDetailViewController: UIViewController {
@IBOutlet weak var organizationNameLabel: UILabel!
var organization: Organizations?
var organizationName: String = ""
override func viewDidLoad() {
super.viewDidLoad()
organizationNameLabel.text = organizationName
}
}
解决方案
做这个实验。改变
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? OrganizationsDetailViewController {
destination.organization = selectedOrganizations
}
}
至
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? OrganizationsDetailViewController {
destination.organizationName = "Zampabalooie"
}
}
你现在看到什么了吗?如果是这样,现在再次更改它,以显示您实际想要查看的文本。
推荐阅读
- java - io.prometheus.client.Gauge 是否可用于线程安全计数器
- javascript - 如何在展示广告中显示动态日期
- spring - 使用 @Bean 注释的 SOAP 客户端的动态配置
- prometheus - 格式化 PromQL 值
- c# - LiveCharts 上的自定义类?
- powershell - 在多个窗口中打开网站并使用 powershell 向下滚动它们
- oracle-apex - Oracle APEX:如何更改交互式网格中开关列的值(使用动态操作 - JavaScript)?
- vb.net - 暂停执行的线程,然后重新使用同一个线程
- php - 表单提交后Codeigniter 4错误404路由有问题
- xml - 我可以在 XML 中使用相同名称和相同值的属性吗?