Is it possible to present different data to the same table view depending on which cell of the previous table view is selected?
For example, the first table view above will already be populated with data, I then want the cells to link to the other table view which should contain items in each cell. At the moment I can only get it to show the same data in the table.
So how would I show the different data within the same table view and present the different data when different cells are selected from the previous view controller? The data is been saved and pulled from core data in the form of an array.
Is this possible or will I need to create different table views ready to be populated by the user?
Here is the code for the first TableViewController:
import UIKit
class ListsViewController: UITableViewController {
let viewController = ListNameViewController()
let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
viewDidAppear(false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(false)
viewController.loadList()
self.tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewController.listName.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath)
let result = viewController.listName[indexPath.row]
cell.textLabel?.text = ("\(String(result.listName!))")
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
context!.delete(viewController.listName[indexPath.row])
viewController.listName.remove(at: indexPath.row)
viewController.saveList()
self.tableView.reloadData()
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "items", sender: self
}
}
Here is the code for the Second View Controller:
import UIKit
import CoreData
class RootListsViewController: UITableViewController {
var titleName = ""
var newListItem : String = ""
var item : [ListItem] = [] //listItems Coredata
let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addItem(_ sender: Any) {
}
@objc func returnHome(sender: UIBarButtonItem) {
performSegue(withIdentifier: "home", sender: self)
}
@objc func returnList(sender: UIBarButtonItem) {
//performSegue(withIdentifier: "list", sender: self)
tabBarController?.selectedIndex = 0
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return item.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
let result = item[indexPath.row]
cell.textLabel?.text = ("\(String(result.name!))")
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//performSegue(withIdentifier: "items2", sender: self)
}
var label: UILabel {
let label = UILabel(frame: tableView.bounds)
label.text = "empty"
return label
}
func saveList() {
do {
try context!.save()
} catch {
print("Error saving context \(error)")
}
}
func loadList() {
let request : NSFetchRequest<ListItem> = ListItem.fetchRequest()
do{
item = try context!.fetch(request)
} catch {
print("Error loading categories \(error)")
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(false)
loadList()
self.tableView.reloadData()
}
}
Sorry if this is a silly question, can't seem to find answers.