【问题标题】:change the text of the Label on TableViewCell更改 TableViewCell 上 Label 的文本
【发布时间】:2017-08-28 18:00:51
【问题描述】:

我有第一个视图控制器(FirstView Controller),它显示了两个 TextFields 和一个 TableView,其中包括 TableViewCell 和 Label。而且我有第二个视图控制器(TypeViewController),它还包括 TableView 和包含一个标签的 TableViewCell。

在 FirstViewController 上选择带有文本的 TableViewCell Choose -> 继续查看 TypeViewController

在 TypeViewController 上选择 TableViewCell(在 TypeViewController 上,我有单元格列表,可以动态加载)-> 返回 FirstViewController,而不是文本 Choose 显示在 TypeViewController 上选择的文本。

如何将 FirstViewController 上 TableViewCell 上的 Label 文本更改为 TypeViewController 中的文本?

第一个视图控制器

import UIKit

class FirstViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {

    var animalTypeName: String?;

    var selectedTypeId: UInt?;
    var selectedTypeName: String?;

    override func viewDidLoad() {
        super.viewDidLoad();
    }

    // MARK: TableView && TableViewCell Methods
    // Row display.
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FirstTableViewCell;
        animalTypeName = "Choose";
        cell.typeName.text = animalTypeName;

        return cell;
    }

    //MARK: - Navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        super.prepare(for: segue, sender: sender)

        switch(segue.identifier ?? "") {
            case "ShowType":
                guard segue.destination is TypeViewController else {
                fatalError("Unexpected destination: \(segue.destination)");
                }
            default:
                fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))");
        }
    }

    //MARK: Actions
    @IBAction func unwindToCreateMedicamentController(sender: UIStoryboardSegue) {
        if let typeViewController = sender.source as? TypeViewController, let type = typeViewController.type {

            selectedTypeName = type.name;
            selectedTypeId = type.animalTypeId;
            animalTypeName = selectedTypeName;
        }
    }
}

TypeViewController

import UIKit

 class TypeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var typeList: [AnimalType]?;
    var type: AnimalType?;
    let typeController = AnimalTypeController();

    override func viewDidLoad() {
        super.viewDidLoad();
        // Call method to get array of AnimalType items.
        getArrayOfTypeItems();
    }

    //MARK: - Table view data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }

    // Row display.
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return typeList?.count ?? 0;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "TypeTVCell";

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TypeTableViewCell  else {
        fatalError("The dequeued cell is not an instance of TypeTVCell.");
    }

        let typeItem = typeList?[indexPath.row];
        cell.nameOfType.text = typeItem!.name;

        return cell;
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        super.prepare(for: segue, sender: sender)

        switch(segue.identifier ?? "") {
        case "ChooseType":
            guard segue.destination is CreateMedicamentController else {
                fatalError("Unexpected destination: \(segue.destination)");
            }

            guard let selectedTypeCell = sender as? TypeTableViewCell else {
                fatalError("Unexpected sender: \(String(describing: sender))");
            }

            guard let indexPath = tableView.indexPath(for: selectedTypeCell) else {
                fatalError("The selected cell is not being displayed by the table");
        }
            let selectedType = typeList?[indexPath.row];
            type = selectedType;

        default:
            fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
        }
    }
}

【问题讨论】:

标签: ios swift uitableview swift3 segue


【解决方案1】:

viewDidLoad方法s中需要添加以下代码:

tableView.dataSource = self
tableView.delegate = self

【讨论】:

    【解决方案2】:
    class FirstViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
    
        var animalTypeName: String?;
    
        var selectedTypeId: UInt?;
        var selectedTypeName: String?;
    
        @IBOutlet weak var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad();
            animalTypeName = "Choose";
        }
    
        // MARK: TableView && TableViewCell Methods
        // Row display.
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1;
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FirstTableViewCell;
            cell.typeName.text = animalTypeName;
    
            return cell;
        }
    
        //MARK: - Navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
            super.prepare(for: segue, sender: sender)
    
            switch(segue.identifier ?? "") {
                case "ShowType":
                    guard segue.destination is TypeViewController else {
                        fatalError("Unexpected destination: \(segue.destination)");
                    }
                default:
                    fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))");
            }
        }
    
        //MARK: Actions
        @IBAction func unwindToCreateMedicamentController(sender: UIStoryboardSegue) {
            if let typeViewController = sender.source as? TypeViewController, let type = typeViewController.type {
    
                selectedTypeName = type.name;
                selectedTypeId = type.animalTypeId;
                animalTypeName = selectedTypeName;
    
                tableView.reloadData();
            }
        }
    }
    
    1. animalTypeName = "Choose"; 移至viewDidLoad
    2. FirstViewController 中添加@IBOutlet weak var tableView: UITableView!
    3. unwindToCreateMedicamentController(sender: UIStoryboardSegue) 中添加tableView.reloadData()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 2022-10-20
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      相关资源
      最近更新 更多