Sunday, May 1, 2016

How to implement, edit and delete behavior in Swift?

Leave a Comment

I have two ViewControllers, one is GoalsViewController and the other one is AddNewGoalViewController.

The GoalsViewController is useful to delete goals (cells) and to add new goals (cells). There is a UITableView and a button, Add new Goal. When the user presses the Add new Goal button, it will pass to AddNewGoalViewController. In AddNewGoalViewController users will select workout, notifications (how many times they want to be notified), and how much they want to run, walk or do any other work.

I checked a tutorial (click on word "tutorial" to check it), and it was helpful. The problem is that is implementing empty cells. Download my project to check it better.

1 Answers

Answers 1

Well, did you check the solution of the exercise?

There is a link at the end of the page ;)

1st Difference:

var workouts = [Workout]()  var numbers = [Workout]()  func loadSampleMeals() {     let workouts1 = Workout(name: "Run", number: "1000")!      let workouts2 = Workout(name: "Walk", number: "2000")!      let workouts3 = Workout(name: "Push-Ups", number: "20")!      workouts += [workouts1, workouts2, workouts3]     numbers += [workouts1, workouts2, workouts3] } 

should be:

var workouts = [Workout]()  func loadSampleMeals() {     let workouts1 = Workout(name: "Run", number: "1000")!      let workouts2 = Workout(name: "Walk", number: "2000")!      let workouts3 = Workout(name: "Push-Ups", number: "20")!      workouts += [workouts1, workouts2, workouts3] } 

2nd Difference:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     // Table view cells are reused and should be dequeued using a cell identifier.     let cellIdentifier = "DhikrTableViewCell"     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell      // Fetches the appropriate meal for the data source layout.     let dhikr = workouts[indexPath.row]     let number = numbers[indexPath.row]       cell.nameLabel.text = dhikr.name     cell.numberLabel.text = number.number     //cell.photoImageView.image = dhikr.photo     //cell.ratingControl.rating = dhikr.rating      return cell } 

should be:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     // Table view cells are reused and should be dequeued using a cell identifier.     let cellIdentifier = "DhikrTableViewCell"     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! GoalsTableViewCell      // Fetches the appropriate meal for the data source layout.     let dhikr = workouts[indexPath.row]       cell.nameLabel.text = dhikr.name     cell.numberLabel.text = dhikr.number     //cell.photoImageView.image = dhikr.photo     //cell.ratingControl.rating = dhikr.rating      return cell } 

3rd Difference:

Where's this? (It doesn't really matter if you are not using NavigationController, but it's a difference between your code and the solution's code).

@IBAction func cancel(sender: UIBarButtonItem) {     // Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.     let isPresentingInAddMealMode = presentingViewController is UINavigationController      if isPresentingInAddMealMode {         dismissViewControllerAnimated(true, completion: nil)     } else {         navigationController!.popViewControllerAnimated(true)     } } 

Those are the differences I spotted between your code and the solution's code ;)

P.S.:

class Workout {     // MARK: Properties      var name: String     //var notifications: Int     var number: Int      // MARK: Initialization      init?(name: String, number: Int) {         // Initialize stored properties.         self.name = name         //self.notifications = notifications         self.number = number          // Initialization should fail if there is no name or if the rating is negative.         if name.isEmpty || number < 0{             return nil         }     } } 

number will never be < 0, perhaps you meant == 0?.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment