Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rodrigo Chousal
16,009 PointsSomething wrong when appending object to array for Table View
When I run my code, everything runs fine without errors. However, when I try to add a cell to my table view using an add button, a segue, and a text field, I am unable to do so. What happens is that when I append the object (custom) to the array that is used to create the cells, the array is emptied and filled again. The result of this is just having one cell at the table view at one time. The code is below..
// Routine.swift:
struct Routine {
var name: String?
var desc: String?
}
// RoutineListViewController.swift:
class RoutineListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var routines = [Routine]()
override func viewDidLoad() {
super.viewDidLoad()
println("There are \(routines.count) routine(s).")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return routines.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("RoutineCell", forIndexPath: indexPath) as UITableViewCell
var cellName = routines[indexPath.row].name!
cell.textLabel?.text = cellName
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
// EditRoutineViewController:
class EditRoutineViewController: UIViewController {
var newRoutine = Routine(name: "", desc: "")
var routineName: String?
var routineDescription: String?
var allTextFields: UITextField?
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var descriptionField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
allTextFields = textField
if (textField.placeholder == "Name") {
if (textField.text != nil) {
routineName = textField.text
println("Routine name set as '\(routineName)'")
}
textField.resignFirstResponder()
return true
} else if (textField.placeholder == "Description") {
if (textField.text != nil) {
routineDescription = textField.text
println("Routine description set as '\(routineDescription!)'")
}
textField.resignFirstResponder()
return true
}
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let navController = segue.destinationViewController as UINavigationController
let RoutineListController = navController.topViewController as RoutineListViewController
println("There are \(RoutineListController.routines.count) routines in the routines array.")
RoutineListController.routines.append(newRoutine)
println("There are \(RoutineListController.routines.count) routines in the routines array.")
println("A new routine called \(newRoutine.name!) has been added to the routines array.")
}
@IBAction func cancel(sender: AnyObject) {
allTextFields?.endEditing(true)
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func done(sender: AnyObject) {
routineName = self.nameField.text
routineDescription = self.descriptionField.text
if (self.nameField.text == "") {
// Display alert view
let nameAlert = UIAlertController(title: "Oops!", message:
"Make sure you enter a name for the routine", preferredStyle: UIAlertControllerStyle.Alert)
nameAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(nameAlert, animated: true, completion: nil)
} else {
println("A routine called \(self.routineName!) is about to be created.")
if (self.routineDescription != nil) {
println("It has a description of '\(self.routineDescription!)'.")
}
var localRoutine = Routine(name: routineName, desc: routineDescription?)
self.newRoutine = localRoutine
view.endEditing(true)
self.performSegueWithIdentifier("showRoutines", sender: nil)
}
}
}
The mistake must be pretty simple, but I don't know what I'm not seeing..