Download presentation
Presentation is loading. Please wait.
Published byΓεώργιος Αγγελίδης Modified over 5 years ago
1
EEC-492/693/793 iPhone Application Development
Lecture 12 Wenbing Zhao & Nigamanth Sridhar 4/9/2019 EEC492/693/793 - iPhone Application Development
2
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
3
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
4
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
5
Datasource Message Flow
numberOfSectionsInTableView: How many Sections? Datasource 5 4/9/2019 EEC492/693/793 - iPhone Application Development
6
Datasource Message Flow
tableView:numberOfRowsInSection: 1 How many rows In section 0? Datasource 4/9/2019 EEC492/693/793 - iPhone Application Development
7
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
8
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
9
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
10
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 if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:... autorelease]; } cell.text = [myStrings objectAtIndex:indexPath.row] return cell; 4/9/2019 EEC492/693/793 - iPhone Application Development
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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 cell.textLabel.text Idol”; cell.detailTextLabel.text Idol”; 4/9/2019 EEC492/693/793 - iPhone Application Development
21
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
22
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
23
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
24
Push Another View Controller on the Stack due to User Actions
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { childController = [[DisclosureDetailController alloc] 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
25
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
26
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.