传递的标签未出现

问题描述 投票:0回答:2

我正在尝试创建一个tableview,在该tableview中,一旦选择了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
}
}
ios swift xcode
2个回答
0
投票

您可以通过将所选的Organisation存储在这样的变量中来传递它:

class SearchOrganizationsViewController: UIViewController {
    var selectedOrganisation: Organization?
    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"])
    ]
    //...
}

extension SearchOrganizationsViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedOrganizations = searching ? searchArray[indexPath.section] : organizations[indexPath.section]
        performSegue(withIdentifier: "organizationDetailSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? OrganizationsDetailViewController {
            destination.organizationName = selectedOrganisation?. sectionTitle
        }
    }
}

1
投票

进行此实验。更改

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

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

© www.soinside.com 2019 - 2024. All rights reserved.