-1

I am making an app to keep track of my homework and for some reason when I call reloadData it doesn't work, some people have already asked about this but I have tried there fixes and they don't work for me, here is the file where I add new homework:

import UIKit
class AddHomework : UIViewController {
     @IBOutlet weak var HomeworkNameLbl: UILabel!
     @IBOutlet weak var HomeworkNameTxt: UITextField!
     @IBOutlet weak var DueDateLbl: UILabel!
     @IBOutlet weak var DueDateTxt: UITextField!
     @IBOutlet weak var DueTimeLbl: UILabel!
     @IBOutlet weak var DueTimeTxt: UITextField!
     @IBOutlet weak var AddHomeworkBtn: UIButton!
     override func viewDidLoad() {
         super.viewDidLoad()
         self.hideKeyboardWhenTappedAround()
     }
     @IBAction func AddHomework(sender: UIButton) {
         let HomeworkName = HomeworkNameTxt.text
         let DueDate = DueDateTxt.text
         let DueTime = DueTimeTxt.text


         homeworkTableView().AddObject([DueDate!, DueTime!],    HomeworkName: HomeworkName!)


}
func hideKeyboardWhenTappedAround() {5
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
    view.endEditing(true)
}


}

Here is the file where I have my tableview:

     import UIKit
     class homeworkTableView: UIViewController, UITableViewDelegate,    UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.reloadData()
}
var homework : [String: [String]] = [
    "Spanish Test": ["Aug 12", "12:00 AM", "Spanish"],
    "Math Sheet": ["Aug 13", "10:30 PM","Math"],
    "Code Thing": ["Aug 11","12:00 AM","Coding"]

]


var titles = [
    "Spanish Test", "Math Sheet", "Code Thing"
]

func AddObject(newArray: [String], HomeworkName: String){
    titles.append(HomeworkName)
    homework.updateValue(newArray, forKey: HomeworkName)

    print(homework)
    print(titles)
}


 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return titles.count
}

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let Cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell

    Cell.Title.text = titles[indexPath.row]
    let currentTitle = titles[indexPath.row]
    let current = homework[currentTitle]!
    Cell.DueDate.text = "Due Date: \(current[0])"
    Cell.Class.text = "Due Time: \(current[1])"

    self.tableView.rowHeight = 100.00

    return Cell
}

}

And the whole project is posted on git here: https://github.com/IndyBob2019/HomeworkHelper

Any help is much appreciated, thanks!

2 Answers2

0

You need to do what you do in viewDidLoad in viewWillAppear. viewDidLoad is only called at startup, so despite changing the data, your view never changes its own local data, so when you call reloadData it still uses the old data.

Taken from tableView reloadData does not work.

Community
  • 1
  • 1
pkozlowski
  • 71
  • 7
0

Here are some pointers based from your code,

First:
You're just calling -reloadData() once in viewDidLoad()..

Second:
If you want to see the changes made, it has to be in this order Update your -dataSource > -reloadData()

ex. from your code...

func AddObject(newArray: [String], HomeworkName: String){
    titles.append(HomeworkName)
    homework.updateValue(newArray, forKey: HomeworkName)

    // check is changes was made
    print(homework) 
    print(titles)  

    // reload after updating datasource
    self.tableView.reloadData() 
}

Edit: After checking your project, i must say that your approach different from what i have in mind.. Calling calling -reloadData() once in viewDidLoad() is correct, but the problem is your table dataSource which was updated inside File.swift by:

homeworkTableView().AddObject([DueDate!, DueTime!], HomeworkName: HomeworkName!)

You initialized homeworkTableView class and added or appended the data within that class but upon presenting the homeworkTableView viewController from navigationController, you are presenting new homeworkTableView class (not the homeworkTableView that was update)..

Calling self.tableView.reloadData() says:

fatal error: unexpectedly found nil while unwrapping an Optional value

because self.tableView was nil during the time of the update,

Here the solution I made:

First:
Globalizing your dataSource for Cross class updating, like so:

import Foundation
import UIKit

// I just moved your variables outside the class
//
var homework : [String: [String]] = [
    "Spanish Test": ["Aug 12", "12:00 AM", "Spanish"],
    "Math Sheet": ["Aug 13", "10:30 PM","Math"],
    "Code Thing": ["Aug 11","12:00 AM","Coding"]

]

var titles = [
    "Spanish Test", "Math Sheet", "Code Thing"
]

class homeworkTableView: UIViewController, UITableViewDelegate, UITableViewDataSource{ 
    ...
    // codes
    ...
}

Second:
Removing self.tableView.reloadData() inside func AddObject(.. which i suggested before (without knowledge of your approach/implementation), like:

func AddObject(newArray: [String], HomeworkName: String){
    titles.append(HomeworkName)
    homework.updateValue(newArray, forKey: HomeworkName)

    // check is changes was made
    print(homework) 
    print(titles)
}
0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • I have tried this and It didn't seem to work, I will try again when I get to my computer. – David Richardson Aug 16 '16 at 13:08
  • It says fatal error: unexpectedly found nil while unwrapping an Optional value – David Richardson Aug 17 '16 at 18:46
  • I know that usually means one of my values is like Optional(...) but I thought I unwrapped all of that with !, I will update my git repository if you could look at that and leave me a comment that would be great – David Richardson Aug 17 '16 at 18:47
  • @DavidRichardson; great! your repo. in git doesn't have the project though. – 0yeoj Aug 18 '16 at 02:45
  • 1
    Odd, for some reason all the files in my repo were deleted, I will try to fix it tomorrow but for some reason I have had a lot of trouble uploading to that repository in general, thanks for being patient with me! – David Richardson Aug 19 '16 at 03:23
  • I'm tagging you because I'm not sure if it auto notifies you – David Richardson Aug 20 '16 at 12:59
  • @DavidRichardson, Sorry for late response, my macbook is broken at the moment.. Check the updated answer, hope it would help you. Cheers! :) – 0yeoj Aug 22 '16 at 05:47
  • This works perfectly man! I kinda gave up on this project because I just couldn't figure it out its funny how easy of a solution this was. – David Richardson Sep 12 '16 at 15:34