EEC-492/693/793 iPhone Application Development Lecture 12 Wenbing Zhao & Nigamanth Sridhar 4/9/2019 EEC492/693/793 - iPhone Application Development
Outline More on table views Using table view & navigation controller together Assignments: The push and pop app Continue building the calculator app 4/9/2019 4/9/2019 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2
EEC492/693/793 - iPhone Application Development More on Table Views UITableView Data Source UITableView Delegate UITableViewController UITableViewCell 4/9/2019 EEC492/693/793 - iPhone Application Development
Basic UITableViewDataSource Methods Provide number of sections and rows // Optional method, defaults to 1 if not implemented - (NSInteger)numberOfSectionsInTableView:(UITableView *)table; // Required method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; Provide cells for table view as needed - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 4/9/2019 EEC492/693/793 - iPhone Application Development
Datasource Message Flow numberOfSectionsInTableView: How many Sections? Datasource 5 4/9/2019 EEC492/693/793 - iPhone Application Development
Datasource Message Flow tableView:numberOfRowsInSection: 1 How many rows In section 0? Datasource 4/9/2019 EEC492/693/793 - iPhone Application Development
Datasource Message Flow tableView:cellForRowAtIndexPath: Cell with text “John Appleseed” What to display at section 0, row 0? Datasource 4/9/2019 EEC492/693/793 - iPhone Application Development
NSIndexPath and Table Views Cell location described with an index path Section index + row index Category on NSIndexPath with helper methods @interface NSIndexPath (UITableView) + (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section; @property(nonatomic,readonly) NSUInteger section; @property(nonatomic,readonly) NSUInteger row; @end 4/9/2019 EEC492/693/793 - iPhone Application Development
Single Section Table View Return the number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myStrings count]; } Provide a cell when requested - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewCell *cell = ...; cell.textLabel.text = [myStrings objectAtIndex:indexPath.row] return [cell autorelease]; 4/9/2019 EEC492/693/793 - iPhone Application Development
EEC492/693/793 - iPhone Application Development Cell Reuse When asked for a cell, reuse one if possible - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:@“MyIdenifier”] autorelease]; } cell.text = [myStrings objectAtIndex:indexPath.row] return cell; 4/9/2019 EEC492/693/793 - iPhone Application Development
EEC492/693/793 - iPhone Application Development Triggering Updates When is the datasource asked for its data? When a row becomes visible When an update is explicitly requested by calling -reloadData - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView reloadData]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
Section and Row Reloading (and Insertion and Deletion) Section reloading - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; Row reloading - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths Insert/delete - (void)insertSections:(NSIndexSet *)sections - (void)deleteSections:(NSIndexSet *)sections - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths 4/9/2019 EEC492/693/793 - iPhone Application Development
Allow Editing and Reordering Cells Enable editing: Call self.tableView setEditing:YES Or use the system provided editButtonItem Allow deleting cells Implement data source method: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; [self.list removeObjectAtIndex:row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
Allow Editing and Reordering Cells Allow reordering cells - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath // update data in the model accordingly … 4/9/2019 EEC492/693/793 - iPhone Application Development
Disallowing Editing/Reordering Disallowing deleting - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } Disallowing reordering - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath return NO; 4/9/2019 EEC492/693/793 - iPhone Application Development
UITableView Delegate: Controlling Table View Appearance & Behavior Customize appearance of table view cell - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; Validate and respond to selection changes - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 4/9/2019 EEC492/693/793 - iPhone Application Development
Responding to Selection // For a navigation hierarchy... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Get the row and the object it represents NSUInteger row = indexPath.row id objectToDisplay = [myObjects objectAtIndex:row]; // Create a new view controller and pass it along MyViewController *myViewController = ...; myViewController.object = objectToDisplay; [self.navigationController pushViewController:myViewController animated:YES]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
Altering or Disabling Selection - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Don’t allow selecting certain rows? if (indexPath.row == ...) { return nil; } else { return indexPath; } 4/9/2019 EEC492/693/793 - iPhone Application Development
UITableViewController Convenient starting point for view controller with a table view Table view is automatically created Controller is table view’s delegate and datasource Takes care of some default behaviors Calls -reloadData the first time it appears Deselects rows when user navigates back Flashes scroll indicators 4/9/2019 EEC492/693/793 - iPhone Application Development
EEC492/693/793 - iPhone Application Development Table View Cells Initializer - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; Cell Styles UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, UITableViewCellStyleValue2 Basic properties UITableViewCell has an image view and one or two text labels cell.imageView.image = [UIImage imageNamed:@“vitolidol.png”]; cell.textLabel.text = @“Vitol Idol”; cell.detailTextLabel.text = @“Billy Idol”; 4/9/2019 EEC492/693/793 - iPhone Application Development
Table View Cell Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { // Only for the blue disclosure button NSUInteger row = indexPath.row; ... } UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailDisclosureButton UITableViewCellAccessoryCheckmark 4/9/2019 EEC492/693/793 - iPhone Application Development
Customizing the Content View For cases where a simple image + text cell doesn’t suffice UITableViewCell has a content view property Add additional views to the content view - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = ...; CGRect frame = cell.contentView.bounds; UILabel *myLabel = [[UILabel alloc] initWithFrame:frame]; myLabel.text = ...; [cell.contentView addSubview:myLabel]; [myLabel release]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
Using Table View and Navigation Controller Together Pushing a TableViewController as the first view controller on the navigation stack - (void)applicationDidFinishLaunching:(UIApplication *)application { UINavigationController *navController = [[UINavigationController alloc] init]; FirstLevelViewController *viewController = [[FirstLevelViewController alloc] init]; [navController pushViewController:viewController animated:NO]; [viewController release]; [window addSubview:navController.view]; [window makeKeyAndVisible]; } Do Not Follow the Book on This Step 4/9/2019 EEC492/693/793 - iPhone Application Development
Push Another View Controller on the Stack due to User Actions - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { childController = [[DisclosureDetailController alloc] initWithNibName:@"DisclosureDetail" bundle:nil]; … [self.navigationController pushViewController:childController animated:YES]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { PresidentDetailController *childController = [[PresidentDetailController alloc] initWithStyle:UITableViewStyleGrouped]; …. [self.navigationController pushViewController:childController animated:YES]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
Pop a View Controller from the Stack Tag the back button When handling user action -(IBAction)cancel:(id)sender{ [self.navigationController popViewControllerAnimated:YES]; } 4/9/2019 EEC492/693/793 - iPhone Application Development
EEC492/693/793 - iPhone Application Development Assignment Build the Nav app Please use the better way to push the first table view controller on the stack Challenge In the current Nav app, the PresentsView is NOT updated after you have modified a President’s info (in the PresidentDetail view) Fix this! 4/9/2019 EEC492/693/793 - iPhone Application Development