apple development technology workshops · 2008-11-04 · building iphone apps. pt 2 hafez rouzati...

44
Fall 2008 Workshop 10 Table Views Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008

Upload: others

Post on 22-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Fall 2008

Workshop 10Table Views

Building iPhone Apps. Pt 2

Hafez Rouzati

Zach Pousman

Apple Development Technology Workshops

Fall 2008

Page 2: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Last WeekUIViewControllers

Organizing Content & Building iPhone Apps with Navigation & Tab Bar Controllers

Page 3: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

TodayWhat is a table view?

Anatomy of a Table View

Classes UITableViewController

Protocols UITableViewDataSource UITableViewDelegate

Page 4: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Views

Page 5: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Views

Page 6: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Views

Page 7: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Views

• Display lists of content! Single column, multiple rows

! Vertical scrolling

! Large data sets

• Powerful and ubiquitous in iPhone applications

Page 8: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table View Styles

UITableViewStylePlain UITableViewStyleGrouped

Page 9: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Cell

Table Footer

Table Header

Section Header

Section Footer

Section

Table View AnatomyPlain Style

Page 10: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table Cell

Table Footer

Table Header

Section Header

Section Footer

Section

Table View AnatomyGrouped Style

Page 11: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Using Table Views

• Displaying your data in the table view

• Customizing appearance & behavior

Page 12: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Displaying Data in a Table View

Page 13: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

A Naïve Solution

• Table views display a list of data, so use an array [myTableView setList:myListOfStuff];

• Issues with this approach! All data is loaded upfront

! All data stays in memory

Page 14: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

A More Flexible Solution

• Another object provides data to the table view! Not all at once

! Just as it’s needed for display

• Like a delegate, but purely data-oriented

Page 15: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

UITableView Datasource

• Provide number of sections and rows

• Provide cells for table view as needed

// Optional method, defaults to 1 if not implemented

- (NSInteger)numberOfSectionsInTableView:(UITableView *)table;

// Required method

- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section;

// Required method

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Page 16: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

NSIndexPath

• Generic class in Foundation

• Path to a specific node in a tree of nested arrays

0

1

2

3

4

0

1

2

3

4

0

1

2

3

4

0

1

2

3

4

Page 17: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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

Page 18: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Single Section Table View

• Return the number of rows

• Provide a cell when requested

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = ...;

cell.text = [myStrings objectAtIndex:indexPath.row]

return [cell autorelease];

}

- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section

{

return [myStrings count];

}

Page 19: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

Page 20: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

How many sections?

numberOfSectionsInTableView:

Page 21: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

numberOfSectionsInTableView:

5

Page 22: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

tableView:numberOfRowsInSection:

How many rows in section 0?

Page 23: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

tableView:numberOfRowsInSection:

1

Page 24: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

What to display at section 0, row 0?

Page 25: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

Cell with text “John Appleseed”

Page 26: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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];

}

Page 27: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Additional Datasource Methods

• Titles for section headers and footers

• Allow editing and reordering cells

Page 28: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Appearance & Behavior

Page 29: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

UITableView Delegate

• Customize appearance and behavior

• Keep application logic separate from view

• Often the same object as datasource

Page 30: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table View Appearance & Behavior

• Customize appearance of table view cell

• Validate and respond to selection changes

- (void)tableView:(UITableView *)tableView

willDisplayCell:(UITableViewCell *)cell

forRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSIndexPath *)tableView:(UITableView *)tableView

willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

Page 31: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Row Selection in Table Views

• In iPhone applications, rows rarely stay selected

• Selecting a row usually triggers an event

Page 32: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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];

}

Page 33: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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;

}

}

Page 34: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

UITableViewController

Page 35: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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

Page 36: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Demo:

UITableViewController

Page 37: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Table View Cells

Page 38: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Basic properties

• UITableViewCell has image and text properties

cell.image = [UIImage imageNamed:@“obama.png”];

cell.text = @“Barack Obama”;

Page 39: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Accessory Types

- (void)tableView:(UITableView *)tableView

accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

// Only for the blue disclosure button

NSUInteger row = indexPath.row;

...

}

// UITableView delegate method

- (UITableViewCellAccessoryType)tableView:(UITableView *)table

accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

Page 40: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

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

- (void)tableView:(UITableView *)tableView

willDisplayCell:(UITableViewCell *)cell

forRowAtIndexPath:(NSIndexPath *)indexPath

{

CGRect frame = cell.bounds;

frame.size.width = ...;

UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];

myLabel.text = ...;

[cell.contentView addSubview:myLabel];

[myLabel release];

}

Page 41: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Custom Row Heights

• Rows in a table view may have variable heights

• NSString category in UIStringDrawing.h is very useful for computing text sizes

- (CGFloat)tableView:(UITableView *)tableView

heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *text = ...;

UIFont *font = [UIFont systemFontOfSize:...];

CGSize withinSize = CGSizeMake(tableView.width, 1000];

CGSize size = [text sizeWithFont:font

constrainedToSize:withinSize

lineBreakMode:UILineBreakModeWordWrap];

return size.height + somePadding;

}

Page 42: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Questions?

With thanks to Paul Marcos (Apple Inc. / Stanford) for the slide deck!

Page 43: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Exercise 10 - TableView-Based Application-Create a new iPhone Window-Based Application project.

-First, copy into the new project the color view controller class we used in the last exercise.

-Next, add a new subclass of UITableViewController. This view controller will manage a table view that displays the names of several colors.

-Implement a viewDidLoad: method to set the table view controller’s title to “Colors”

-In the application delegate, in the applicationDidFinishLaunching: method, create an instance of of UINavigationController and an instance of the table view controller (initialize it using the plain style). Push the table view controller as the first controller for the navigation controller.

-Add the navigation controller’s view as a subview of the application’s window.

Page 44: Apple Development Technology Workshops · 2008-11-04 · Building iPhone Apps. Pt 2 Hafez Rouzati Zach Pousman Apple Development Technology Workshops Fall 2008. ... • Keep application

Exercise 10In the table view controller, add a property that is an array that will contain information about colors.

In its initializer method, create the array to contain the colors information. Color information is represented by a dictionary with two keys, ‘name’, with a corresponding string value representing the name of a color, and ‘color’, where the corresponding value is a color object of the appropriate color:

dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [UIColor redColor], @”color”, @”Red”, @”name”, nil];

Create dictionaries for several colors and add them to the array.

Implement the table source delegate and data source methods to return appropriate values. (Note: -There is only one section in the table view. -There are as many rows as there are elements in the color information array. -In the tableView:cellForRowAtIndexPath: method, return a UITableViewCell instance with its text set to the name of the color at the releveant index)

Implement a tableView:didSelectRowAtIndexPath: method to create and push a new color view controller, configured using the color information from the dictionary at the corresponding location in the array.