【发布时间】:2020-06-18 14:51:10
【问题描述】:
我在这方面花了相当多的时间,但似乎仍然无法弄清楚我做错了什么。
我想在我自己的函数中在 tableViewController 之外的另一个视图控制器上运行 reloadData() 函数,但我收到错误 Type 'HomeViewController' has no member 'reloadData'。
HomeViewController:
import UIKit
import CoreData
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var index = ""
var item : [ListItem] = [] //listName
var listName = [""]
override func viewDidLoad() {
super.viewDidLoad()
self.homeListsTableView.delegate = self
self.homeListsTableView.dataSource = self
homeListsTableView.reloadData()
//List Names
//List Items - Within a list Name
let listItem1 = ListItem()
listItem1.name = "" //Update so names are updated via append the ListItem Array
listItem1.location = ""
item.append(listItem1)
let listItem2 = ListItem()
listItem2.name = ""
listItem2.location = ""
item.append(listItem2)
}
@IBOutlet weak var homeListsTableView: UITableView!
@IBAction func templatesButton(_ sender: Any) {
tabBarController?.selectedIndex = 2
}
override func viewWillAppear(_ animated: Bool) {
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
if let coreDataListItems = try? context.fetch(ListName.fetchRequest()) as? [ListName] {
print(coreDataListItems)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = listName[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ListNameViewController
vc.homeListsTableViewVC = homeListsTableView
}
}
ListName 视图控制器:
import UIKit
import CoreData
class ListNameViewController: UIViewController, UITableViewDelegate {
// var listName = [""]
let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
var homeListsTableViewVC = HomeViewController.self
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var listNameValue: UITextField!
@IBOutlet weak var locationOption: UITextField!
@IBOutlet weak var createButtonChange: UIButton!
@IBAction func createButton(_ sender: Any) {
let newList = ListName(context: context!)
newList.listName = listNameValue.text
// let location = locationOption.text!
//tabBarController?.selectedIndex = 0
//performSegue(withIdentifier: "home", sender: nil)
}
func saveList() {
do {
try context!.save()
} catch {
print("Error saving context \(error)")
}
homeListsTableViewVC.reloadData()
}
}
我正在尝试使用 prepare for segue 函数来传递 homeListsTableView 数据,然后使用 reloadData() 函数。谁能告诉我我做错了什么?
谢谢!
【问题讨论】:
-
这不是将 UI 元素传递给其他控制器的好方法 ...
-
啊好吧 - 那么我将如何在另一个视图控制器中使用 reloadData() 函数,或者我不会?我应该尝试在其他视图控制器中的某个地方调用它吗?
-
我发布了一个答案...您可以尝试委托或闭包来更新您的视图
标签: swift uitableview segue