Connor Brough Connor Brough - 5 months ago 13
iOS Question

How can I reload tableview within my TableViewController within button action?

Currently I have the following code which saves an object however I am wanting to update/reload the tableview. The button isn't attached to a cell/row it's top right within my navigation controller (plus icon)

Note: Everything is happening within the same scene therefore any events attached to segue where I could reload table data is out of the question.

@IBAction func addWeek (sender: UIButton){

let newnumber:Int = routineWeeks.count + 1

// save data using cor data managed object context
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
week = NSEntityDescription.insertNewObjectForEntityForName("Weeks", inManagedObjectContext: managedObjectContext) as! Weeks

week.weekNumber = newnumber

do {
try managedObjectContext.save()
} catch {
print(error)
return
}
}

//currently not reloading table with new data
tableView.reloadData()

//print information in console

print("end of save function, dismiss controller")

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

// Configure the cell...
cell.textLabel?.text = "Week \(routineWeeks[indexPath.row].weekNumber)"

return cell
}


UPDATE
Thanks Stackoverflow community for pointing me in the right direction.

routineWeeks.append(week)
print("this is a new week:\(week)")
tableView.reloadData()

Answer
routineWeeks.append(week)
print("this is a new week:\(week)")
tableView.reloadData()