我遇到这个问题,我想从一个主列表移到它的子列表,例如,当我单击tableViewController中的面包店时,应该将我带到另一个具有相关食品(例如蛋糕,饼干等)的tableViewController。但是我无法从表格视图1传递到表格视图2。
我尝试使用segues,但是它不起作用。我尝试使用原型进行链接,但这也失败了,它不会切换到下一个表格视图 导入UIKit 导入CoreData
ViewController类:UIViewController,UITableViewDelegate {
var name = "-1"
@IBOutlet weak var tableView: UITableView!
var list: [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
title = "Lists"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// performSegue(withIdentifier: "show", sender: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "List")
do {
list = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
@IBAction func addName(_ sender: Any) {
let alert = UIAlertController(title: "New list", message: "Create a new list", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default) {
[unowned self] action in
guard let textField = alert.textFields?.first,
let nameToSave = textField.text else {
return
}
self.save(name: nameToSave)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addTextField()
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true)
}
func save(name: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "List", in: managedContext)!
let listItem = NSManagedObject(entity: entity, insertInto: managedContext)
listItem.setValue(name, forKeyPath: "name")
do {
try managedContext.save()
list.append(listItem)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
func delete(name: String) {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest<List> = List.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "name = %@", "\(name)")
do {
let objects = try managedContext.fetch(fetchRequest)
for object in objects {
managedContext.delete(object)
}
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let listItem = list[indexPath.row]
name = (listItem.value(forKey: "name") as? String)!
self.performSegue(withIdentifier: "show", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "show" {
let dest = segue.destination as! SecondViewController
dest.segName = name
}
}
}
扩展ViewController:UITableViewDataSource { func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int { 返回list.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let listItem = list[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = listItem.value(forKeyPath: "name") as? String
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let listItem = list[indexPath.row]
let listItemName: String = listItem.value(forKey: "name") as? String ?? "-1"
delete(name: listItemName)
}
}
}
// 2nd表格视图控制器 导入UIKit
SecondViewController类:UIViewController {
var segName = String()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}