2017-08-28 99 views
-1

我有第一個視圖控制器(FirstView控制器)上提供兩個TextFields和一個TableView包含帶有標籤的TableViewCell。我有第二個視圖控制器(TypeViewController),其中還包括TableView與TableViewCell,其中包括一個標籤。更改TableViewCell上的標籤的文本

在FirstViewController選擇TableViewCell文本選擇 - >去查看TypeViewController

在TypeViewController選擇TableViewCell(上TypeViewController我有小區列表,即加載動態) - >回到FirstViewController和替代文本選擇顯示在TypeViewController上選擇的文字。

如何將FirstViewController上的TableViewCell上的標籤的文本更改爲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))") 
     } 
    } 
} 

This`s an example of my views

+0

需要設置委託。 –

+0

這必須是SO上最常見的iOS問題之一。 https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef – toddg

回答

0
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. 添加@IBOutlet weak var tableView: UITableView!FirstViewController
  3. 添加tableView.reloadData()unwindToCreateMedicamentController(sender: UIStoryboardSegue)
1

viewDidLoad方法小號您甲腎上腺素編到如下因素的代碼添加:

tableView.dataSource = self 
tableView.delegate = self