Skip to content
Browse files

Add and display ingredients.

  • Loading branch information...
1 parent 93bf8b3 commit c6c39d38be9b1a2702cb91e952ed2d19fb60b1d4 @alloy committed Aug 9, 2012
View
80 examples/Recipes/app/controllers/ingredient_detail_view_controller.rb
@@ -0,0 +1,80 @@
+class NSIndexPath
+ def self.[](section, row)
+ indexPathForRow(row, inSection:section)
+ end
+end
+
+class IngredientDetailViewController < UITableViewController
+ attr_accessor :recipe, :ingredient, :editingTableViewCell
+
+ def initWithStyle(style)
+ if super
+ navigationItem.title = 'Ingredient'
+ navigationItem.leftBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemCancel,
+ target:self,
+ action:'cancel:')
+ navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemSave,
+ target:self,
+ action:'save:')
+ end
+ self
+ end
+
+ def viewDidLoad
+ super
+ tableView.allowsSelection = tableView.allowsSelectionDuringEditing = false
+ end
+
+ def shouldAutorotateToInterfaceOrientation(orientation)
+ orientation != UIInterfaceOrientationPortraitUpsideDown
+ end
+
+ # UITableView data source
+
+ def tableView(tableView, numberOfRowsInSection:section)
+ 2
+ end
+
+ def tableView(tableView, cellForRowAtIndexPath:indexPath)
+ unless cell = tableView.dequeueReusableCellWithIdentifier('IngredientsCell')
+ NSBundle.mainBundle.loadNibNamed('EditingTableViewCell', owner:self, options:nil)
+ cell, @editingTableViewCell = @editingTableViewCell, nil
+ end
+ case indexPath.row
+ when 0
+ cell.label.text = 'Ingredient'
+ cell.textField.text = @ingredient.name if @ingredient
+ cell.textField.placeholder = 'Name'
+ when 1
+ cell.label.text = cell.textField.placeholder = 'Amount'
+ cell.textField.text = @ingredient.amount if @ingredient
+ end
+ cell
+ end
+
+ # Save and cancel
+
+ def save(sender)
+ unless @ingredient
+ # TODO
+ #@ingredient = @recipe.ingredients.new
+ @ingredient = Ingredient.new(:displayOrder => @recipe.ingredients.count)
+ @recipe.addIngredientsObject(@ingredient)
+ end
+
+ @ingredient.name = tableView.cellForRowAtIndexPath(NSIndexPath[0, 0]).textField.text
+ @ingredient.amount = tableView.cellForRowAtIndexPath(NSIndexPath[0, 1]).textField.text
+
+ # TODO
+ error = Pointer.new(:object)
+ unless MotionData::Context.main.save(error)
+ raise "Unresolved error: #{error[0].debugDescription}"
+ end
+
+ navigationController.popViewControllerAnimated(true)
+ end
+
+ def cancel(sender)
+ navigationController.popViewControllerAnimated(true)
+ end
+end
View
0 ...Recipes/app/recipe_add_view_controller.rb → ...controllers/recipe_add_view_controller.rb
File renamed without changes.
View
2 ...ipes/app/recipe_detail_view_controller.rb → ...trollers/recipe_detail_view_controller.rb
@@ -120,7 +120,7 @@ def tableView(tableView, cellForRowAtIndexPath:indexPath)
if indexPath.section == INGREDIENTS_SECTION
if indexPath.row < @recipe.ingredients.count
unless cell = tableView.dequeueReusableCellWithIdentifier('IngredientsCell')
- cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:'IngredientsCell')
+ cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:'IngredientsCell')
cell.accessoryType = UITableViewCellAccessoryNone
end
ingredient = @ingredients[indexPath.row]
View
0 .../app/recipe_list_table_view_controller.rb → ...lers/recipe_list_table_view_controller.rb
File renamed without changes.
View
0 ...pes/app/type_selection_view_controller.rb → ...rollers/type_selection_view_controller.rb
File renamed without changes.
View
0 examples/Recipes/app/models.rb → examples/Recipes/app/models/models.rb
File renamed without changes.
View
3 examples/Recipes/app/views/editing_table_view_cell.rb
@@ -0,0 +1,3 @@
+class EditingTableViewCell < UITableViewCell
+ attr_accessor :label, :textField
+end
View
0 ...les/Recipes/app/recipe_table_view_cell.rb → ...cipes/app/views/recipe_table_view_cell.rb
File renamed without changes.

0 comments on commit c6c39d3

Please sign in to comment.
Something went wrong with that request. Please try again.