Files
FoodTracker/ChineseFoodTracker/MealTableViewController.swift

123 lines
4.0 KiB
Swift

//
// MealTableViewController.swift
// ChineseFoodTracker
//
// Created by Junv on 7/7/15.
// Copyright © 2015 goldendata. All rights reserved.
//
import UIKit
class MealTableViewController: UITableViewController {
//MARK: properties
var meals = [Meal]()
override func viewDidLoad() {
super.viewDidLoad()
loadSampleMeals()
}
func loadSampleMeals(){
let photo1 = UIImage(named: "meal1.jpg")!
let meal1 = Meal(name: "松花蛋", rating: 4, photo: photo1)!
let photo2 = UIImage(named: "meal2.jpg")!
let meal2 = Meal(name: "回锅肉", rating: 3, photo: photo2)!
let photo3 = UIImage(named: "meal3.jpg")!
let meal3 = Meal(name: "pizza", rating: 2, photo: photo3)!
meals += [meal1, meal2, meal3]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return meals.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellId = "MealTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! MealTableViewCell
let meal = meals[indexPath.row]
cell.mealLabel.text = meal.name
cell.photoImageView.image = meal.photo
cell.ratingControl.rating = meal.rating
return cell
}
@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? MealViewController, meal = sourceViewController.meal {
//add new meal
let newIndexPath = NSIndexPath(forRow: meals.count, inSection: 0)
meals.append(meal)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}