更改TableViewCell上的标签的文本
问题描述:
我有第一个视图控制器(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))")
}
}
}
答
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();
}
}
}
- 移动
animalTypeName = "Choose";
到viewDidLoad
。 - 添加
@IBOutlet weak var tableView: UITableView!
在FirstViewController
- 添加
tableView.reloadData()
在unwindToCreateMedicamentController(sender: UIStoryboardSegue)
答
在viewDidLoad
方法小号您甲肾上腺素编到如下因素的代码添加:
tableView.dataSource = self
tableView.delegate = self
需要设置委托。 –
这必须是SO上最常见的iOS问题之一。 https://medium.com/@jamesrochabrun/implementing-delegates-in-swift-step-by-step-d3211cbac3ef – toddg