1111#import " IHWAppDelegate.h"
1212#import " IHWScheduleViewController.h"
1313
14- @interface IHWChangeYearViewController ()
15-
16- @end
17-
1814@implementation IHWChangeYearViewController
1915
2016- (id )initWithStyle : (UITableViewStyle)style
@@ -25,7 +21,6 @@ - (id)initWithStyle:(UITableViewStyle)style
2521 self.tableView .delegate = self;
2622 self.tableView .dataSource = self;
2723 self.navigationItem .title = @" Change Year" ;
28- // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
2924 self.selectedCampus = [IHWCurriculum currentCampus ];
3025 }
3126 return self;
@@ -50,9 +45,11 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
5045- (UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath
5146{
5247 if (indexPath.section == 0 ) {
48+ // Create the "change year" cell
5349 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @" changeYear" ];
5450 if (cell==nil ) cell = [[UITableViewCell alloc ] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @" changeYear" ];
5551 if (self.stepper == nil ) {
52+ // Create a stepper
5653 self.stepper = [[UIStepper alloc ] init ];
5754 self.stepper .minimumValue = 1 ;
5855 self.stepper .maximumValue = INT16_MAX;
@@ -61,42 +58,56 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
6158 self.stepper .tintColor = [UIColor colorWithRed: 0.6 green: 0 blue: 0 alpha: 1 ];
6259 [self .stepper addTarget: self action: @selector (yearChanged ) forControlEvents: UIControlEventValueChanged];
6360 }
61+ // Add the stepper to the right side of the cell
6462 cell.accessoryView = self.stepper ;
6563 cell.textLabel .text = [self formatYear: [IHWCurriculum currentYear ]];
6664 return cell;
6765 } else {
66+ // Create the "change campus" cells
6867 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @" changeCampus" ];
6968 if (cell==nil ) cell = [[UITableViewCell alloc ] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @" changeCampus" ];
7069 cell.tintColor = [UIColor colorWithRed: 0.6 green: 0 blue: 0 alpha: 1 ];
7170 if (indexPath.row == 0 ) {
7271 cell.textLabel .text = @" Middle School" ;
73- if (self.selectedCampus == CAMPUS_MIDDLE) cell.accessoryType = UITableViewCellAccessoryCheckmark;
72+ if (self.selectedCampus == CAMPUS_MIDDLE) {
73+ // Add a checkmark if necessary
74+ cell.accessoryType = UITableViewCellAccessoryCheckmark;
75+ }
7476 } else {
7577 cell.textLabel .text = @" Upper School" ;
76- if (self.selectedCampus == CAMPUS_UPPER) cell.accessoryType = UITableViewCellAccessoryCheckmark;
78+ if (self.selectedCampus == CAMPUS_UPPER) {
79+ // Add a checkmark if necessary
80+ cell.accessoryType = UITableViewCellAccessoryCheckmark;
81+ }
7782 }
7883 return cell;
7984 }
8085}
8186
8287- (CGFloat)tableView : (UITableView *)tableView heightForHeaderInSection : (NSInteger )section {
88+ // Adjust the spacing before and between cells
8389 if (section == 0 ) return 44 ;
8490 else return 24 ;
8591}
8692
8793- (UIView *)tableView : (UITableView *)tableView viewForHeaderInSection : (NSInteger )section {
88- UITableViewHeaderFooterView *view = [[UITableViewHeaderFooterView alloc ] initWithReuseIdentifier: @" changeScheduleHeader" ];
94+ UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier: @" changeScheduleHeader" ];
95+ if (view == nil ) {
96+ view = [[UITableViewHeaderFooterView alloc ] initWithReuseIdentifier: @" changeScheduleHeader" ];
97+ }
8998 if (section == 0 ) view.textLabel .text = @" Year" ;
9099 else view.textLabel .text = @" Campus" ;
91100 return view;
92101}
102+
93103- (NSIndexPath *)tableView : (UITableView *)tableView willSelectRowAtIndexPath : (NSIndexPath *)indexPath {
94104 if (indexPath.section == 0 ) return nil ;
95105 else return indexPath;
96106}
97107
98108- (void )tableView : (UITableView *)tableView didSelectRowAtIndexPath : (NSIndexPath *)indexPath {
99109 if (indexPath.section == 1 ) {
110+ // Handle changing campus
100111 if (indexPath.row == 0 ) self.selectedCampus = CAMPUS_MIDDLE;
101112 else self.selectedCampus = CAMPUS_UPPER;
102113 [self campusChanged ];
@@ -107,10 +118,12 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
107118
108119- (void )yearChanged {
109120 int year = self.stepper .value ;
121+ // Update year cell text
110122 [self .tableView cellForRowAtIndexPath: [NSIndexPath indexPathForItem: 0 inSection: 0 ]].textLabel .text = [self formatYear: year];
111123}
112124
113125- (void )campusChanged {
126+ // Update checkmarks
114127 if (self.selectedCampus == CAMPUS_MIDDLE) {
115128 [self .tableView cellForRowAtIndexPath: [NSIndexPath indexPathForItem: 0 inSection: 1 ]].accessoryType = UITableViewCellAccessoryCheckmark;
116129 [self .tableView cellForRowAtIndexPath: [NSIndexPath indexPathForItem: 1 inSection: 1 ]].accessoryType = UITableViewCellAccessoryNone;
@@ -124,65 +137,17 @@ - (NSString *)formatYear:(int)year {
124137 return [NSString stringWithFormat: @" %d -%02d " , year, (year+1 )%100 ];
125138}
126139
127- -(void ) viewWillDisappear : (BOOL )animated {
140+ - (void )viewWillDisappear : (BOOL )animated {
128141 if ([self .navigationController.viewControllers indexOfObject: self ]==NSNotFound ) {
142+ // Save updated campus and year
129143 int year = self.stepper .value ;
130144 [IHWCurriculum setCurrentYear: year];
131145 [IHWCurriculum setCurrentCampus: self .selectedCampus];
146+ // Create a new schedule view controller
147+ // (this is not shown immediately -- instead, it appears behind the preferences view controller when the preferences view controller is dismissed)
132148 [((IHWAppDelegate *)[[UIApplication sharedApplication ] delegate ]).navController setViewControllers: @[[[IHWScheduleViewController alloc ] initWithNibName: @" IHWScheduleViewController" bundle: nil ]] animated: NO ];
133- // [self.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
134149 }
135150 [super viewWillDisappear: animated];
136151}
137152
138-
139- /*
140- // Override to support conditional editing of the table view.
141- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
142- {
143- // Return NO if you do not want the specified item to be editable.
144- return YES;
145- }
146- */
147-
148- /*
149- // Override to support editing the table view.
150- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
151- {
152- if (editingStyle == UITableViewCellEditingStyleDelete) {
153- // Delete the row from the data source
154- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
155- } else if (editingStyle == UITableViewCellEditingStyleInsert) {
156- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
157- }
158- }
159- */
160-
161- /*
162- // Override to support rearranging the table view.
163- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
164- {
165- }
166- */
167-
168- /*
169- // Override to support conditional rearranging of the table view.
170- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
171- {
172- // Return NO if you do not want the item to be re-orderable.
173- return YES;
174- }
175- */
176-
177- /*
178- #pragma mark - Navigation
179-
180- // In a storyboard-based application, you will often want to do a little preparation before navigation
181- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
182- {
183- // Get the new view controller using [segue destinationViewController].
184- // Pass the selected object to the new view controller.
185- }
186- */
187-
188153@end
0 commit comments