-
Notifications
You must be signed in to change notification settings - Fork 0
/
IngridientTableViewController.m
255 lines (171 loc) · 7.05 KB
/
IngridientTableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// IngridientTableViewController.m
// WhatToEat
//
// Created by Creonopoulos Creon on 3/29/13.
// Copyright (c) 2013 apt.gr. All rights reserved.
//
#import "IngridientTableViewController.h"
#import "IngredientCell.h"
#import "RecipeListViewController.h"
@interface IngridientTableViewController ()
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
@end
@implementation IngridientTableViewController
@synthesize Ingridients,object,TableView,SelectionsArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//set the title of the view
self.navigationItem.title = @"Select Ingredients";
//navigation bar color
self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];
//make the selection index array all false
for (int i = 0; i < 41; i++) {
switchStateArr[i]=0;
}
selectedIndexes = [[NSMutableDictionary alloc] init];
// Uncomment the following line to preserve selection between presentations.
//give the plist a name
NSString *plistFile = [[NSBundle mainBundle]
pathForResource:@"Ingredients" ofType:@"plist"];
// allocate and store items in the plist to the array property
Ingridients = [[NSMutableArray alloc] initWithContentsOfFile:plistFile];
SelectionsArray = [[NSMutableArray alloc] init];
self.navigationController.toolbarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated{
self.navigationController.toolbarHidden = YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [Ingridients count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableViewCell = @"IngredientCell";
IngredientCell *cell = (IngredientCell*)[tableView dequeueReusableCellWithIdentifier:tableViewCell];
object = [Ingridients objectAtIndex:indexPath.row];
//initialize the selection switch
UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
if(cell == nil){
cell = [[IngredientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCell];
//set the switch state according to the switch state array when cell is null
theSwitch.on = [[NSNumber numberWithInt:switchStateArr[indexPath.row]] boolValue];
}
//configure the cell...
[theSwitch addTarget:self action:@selector(SwitchToggle:) forControlEvents:UIControlEventTouchUpInside];
theSwitch.on = [[NSNumber numberWithInt:switchStateArr[indexPath.row]] boolValue];
cell.accessoryView = theSwitch;
cell.Ingredient.text = [object objectForKey:@"Ingredient"];
cell.IngredImage.image = [UIImage imageNamed:[object objectForKey:@"Image"]];
//Some Ingredients need subtitles
switch (indexPath.row) {
case 5:
cell.Subtitle.text = @"*Includes hot dogs";
break;
case 8:
cell.Subtitle.text = @"*Depends on type";
break;
case 9:
cell.Subtitle.text = @"*Or margarine";
break;
case 11:
cell.Subtitle.text = @"*Depends on type";
break;
case 17:
cell.Subtitle.text = @"*Depends on type";
break;
case 31:
cell.Subtitle.text = @"*Includes Jalepeno Peppers";
break;
default:
cell.Subtitle.text = @"";
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
CGFloat kCellHeight = 60.0f;
return kCellHeight;
}
#pragma mark - Table view delegate
//Left here in case of later need needed app update
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:FALSE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
//sets the value of the switch to the specific index of the mutable array
[selectedIndexes setObject:selectedIndex forKey:indexPath];
// This is where magic happens...
[tableView beginUpdates];
[tableView endUpdates];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
//Function to make the selected ingredient available
- (void)SwitchToggle:(UISwitch *)sender{
//get the row it was sent from
UIView *view = sender;
while (![view isKindOfClass:[IngredientCell class]]) {
view = [view superview];
}
IngredientCell *cell = (IngredientCell *)view;
NSIndexPath *indexPath = [TableView indexPathForCell:cell];
//if the switch is on and the recipe does not already exist in selection array
if(sender.isOn && ![SelectionsArray containsObject:[[Ingridients objectAtIndex:indexPath.row] objectForKey:@"Ingredient"]])
{
//add recipe to array
[SelectionsArray addObject:[[Ingridients objectAtIndex:indexPath.row] objectForKey:@"Ingredient"]];
//set the state of the specific row to on
switchStateArr[indexPath.row]= 1;
}
else if(!sender.isOn)
{
//remove the recipe from the selection array
[SelectionsArray removeObjectIdenticalTo:[[Ingridients objectAtIndex:indexPath.row] objectForKey:@"Ingredient"]];
//set the state of the specific row to off
switchStateArr[indexPath.row]= 0;
}
}
//Prepare the next view before it is pushed in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"RecipeListSegue"]){
//set the recipe list to the destination controller
RecipeListViewController *rvc = [segue destinationViewController];
//set the ingredients of the selected array to the ingredients for the next view controller
rvc.Ingredients = SelectionsArray;
}
}
//method to dimiss the view if the cancel button is selected
- (IBAction)CancelButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end