首页 > 解决方案 > 通过的标签不出现

问题描述

我正在尝试创建一个表格视图,其中一旦选择了一个表格视图单元格,它将单元格的名称传递给一个新的视图控制器。当我运行它并选择一个 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
    }
}

标签: iosswiftxcode

解决方案


做这个实验。改变

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

你现在看到什么了吗?如果是这样,现在再次更改它,以显示您实际想要查看的文本。


推荐阅读