I am confused to why my table view is not reloading after i call the tableView.reloadData() function. Here is what you should know about my project. The initial view controller is a tableViewController and when you click the add button in the navigation bar it pulls up presents the "addItemViewController". It is presented overCurrentContext. Everything to this point works fine, but the part that doesn't work is when you fill out the info in the pop up I created and push the button to save it it saves to the core data fill but when i reload it it doesnt even call that. When i close the app and reload it the data shows up but it doesnt show up when i add it and call the same function.
import UIKit
import CoreData
protocol reloadTableView: class {
func reloadTableView()
}
class TableViewController: UITableViewController {
//Global Variables
let addItemVC = AddItemController()
var itemArray = [Item]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
addItemVC.delegate = self
loadItems()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.cellIdentifier, for: indexPath) as! Cell
let array = itemArray[indexPath.row]
cell.dateCreated.text = array.dateCreated
cell.workoutLabel.text = array.workoutName
cell.weightLifted.text = array.weight
return cell
}
//MARK: - Add Button Pressed
@IBAction func addItemPressed(_ sender: UIBarButtonItem) {
let storyboard = UIStoryboard(name: "AddItem", bundle: nil)
let addItemVC = storyboard.instantiateViewController(identifier: "AddItemController")
addItemVC.isModalInPresentation = true
addItemVC.modalPresentationStyle = .overCurrentContext
addItemVC.modalTransitionStyle = .crossDissolve
addItemVC.navigationController?.isNavigationBarHidden = true
present(addItemVC, animated: true, completion: nil)
}
//MARK: - Create and Load Functions
func saveData() {
do {
try context.save()
} catch {
print("Error Saving Data \(error)")
}
tableView.reloadData()
}
func loadItems() {
let request: NSFetchRequest<Item> = Item.fetchRequest()
do {
itemArray = try context.fetch(request)
} catch {
print("error")
}
tableView.reloadData()
}
}
//MARK:// - Add Item Vc Delegate
extension TableViewController: reloadTableView {
func reloadTableView() {
do {
try context.save()
} catch {
print("Error Saving Data \(error)")
}
let request: NSFetchRequest<Item> = Item.fetchRequest()
do {
itemArray = try context.fetch(request)
} catch {
print("error")
}
tableView.reloadData()
print("There are", itemArray.count, "in the item array")
print(itemArray.last?.workoutName)
//the print statement are not showing up in console
}
}
and the second file
import UIKit
import CoreData
class AddItemController: UIViewController {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var delegate: reloadTableView?
@IBOutlet weak var viewContainer: UIView!
@IBOutlet weak var exercise: UITextField!
@IBOutlet weak var weight: UITextField!
@IBOutlet weak var reps: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func addMaxPressed(_ sender: UIButton) {
if exercise.text != "" && weight.text != "" && reps.text != "" {
let newItem = Item(context: context)
let formatter = DateFormatter()
newItem.dateCreated = formatter.formattedDate()
newItem.weight = weight.text
newItem.reps = reps.text
newItem.workoutName = exercise.text
dismiss(animated: true) {
self.delegate?.reloadTableView()
}
}
}
@IBAction func exitPressed(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
//MARK: - UITextField func
extension AddItemController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
}