56
CS193P - Lecture 9 iPhone Application Development T able Views

Standford CS 193P: 09-Table Views

Embed Size (px)

Citation preview

Page 1: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 1/56

CS193P - Lecture 9

iPhone Application Development

Table Views

Page 2: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 2/56

Announcements

• Grades & feedback will be delivered via email from now on

• Presence 1 is due this Wednesday at 11:59pm

Page 3: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 3/56

Today’s Topics

• Scroll views

• Table views! Displaying data

! Controlling appearance & behavior

• UITableViewController• Table view cells

• Presence - Part 2

Page 4: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 4/56

Scroll Views

Page 5: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 5/56

Page 6: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 6/56

Scrolling Examples

Page 7: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 7/56

Using a Scroll View

• Create with the desired frame

• Add subviews (frames may extend beyond scroll view frame)

• Set the content size

scrollView.contentSize = CGSizeMake(500, 500);

CGRect frame = CGRectMake(0, 0, 200, 200);

scrollView = [[UIScrollView alloc] initWithFrame:frame];

frame = CGRectMake(0, 0, 500, 500);myImageView = [[UIImageView alloc] initWithFrame:frame];

[scrollView addSubview:myImageView];

Page 8: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 8/56

Frame and Content

scrollView.frame

Page 9: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 9/56

Frame and Content

scrollView.contentSize

Page 10: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 10/56

Frame and Content

scrollView.contentOffset

Page 11: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 11/56

Demo:

Using a UIScrollView

Page 12: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 12/56

Extending Scroll View Behavior

• Applications often want to know about scroll events! When the scroll offset is changed

! When dragging begins & ends

! When deceleration begins & ends

Page 13: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 13/56

Page 14: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 14/56

Extending with Delegation

• Delegate is a separate object

• Clearly defined points of responsibility! Change behavior

! Customize appearance

• Loosely coupled with the object being extended

Page 15: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 15/56

UIScrollView Delegate

@protocol UIScrollViewDelegate<NSObject>

@optional

// Respond to interesting events

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

...

// Influence behavior

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

@end

Page 16: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 16/56

Implementing a Delegate

• Conform to the delegate protocol

• Implement all required methods and any optional methods

@interface MyController : NSObject <UIScrollViewDelegate>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{// Do something in response to the new scroll position

if (scrollView.contentOffset ...) {

}

}

Page 17: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 17/56

Zooming with a Scroll View

• Set the minimum, maximum, initial zoom scales

• Implement delegate method for zooming

scrollView.maximumZoomScale = 2.0;

scrollView.minimumZoomScale = scrollView.size.width /

myImage.size.width;

- (UIView *)viewForZoomingInScrollView:(UIView *)view

{

return someViewThatWillBeScaled;

}

Page 18: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 18/56

Page 19: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 19/56

Table Views

Page 20: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 20/56

Table Views

• Display lists of content! Single column, multiple rows

! Vertical scrolling

! Large data sets

• Powerful and ubiquitous in iPhone applications

Page 21: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 21/56

Table View Styles

UITableViewStylePlain UITableViewStyleGrouped

Page 22: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 22/56

Page 23: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 23/56

 Table Cell

 Table Footer

 Table Header

Section Header

Section Footer

Section

Table View AnatomyGrouped Style

Page 24: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 24/56

Using Table Views

• Displaying your data in the table view

• Customizing appearance & behavior

Page 25: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 25/56

Displaying Data in a Table View

Page 26: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 26/56

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 27: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 27/56

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 28: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 28/56

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 29: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 29/56

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 30: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 30/56

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 31: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 31/56

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 32: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 32/56

Datasource Message Flow

Datasource

Page 33: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 33/56

Datasource Message Flow

Datasource

How many

sections?

numberOfSectionsInTableView:

Page 34: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 34/56

Datasource Message Flow

Datasource

numberOfSectionsInTableView:

5

Page 35: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 35/56

Page 36: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 36/56

Datasource Message Flow

Datasource

tableView:numberOfRowsInSection:

1

Page 37: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 37/56

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

What to display at

section 0, row 0?

Page 38: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 38/56

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

Cell with text“John Appleseed”

Page 39: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 39/56

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 40: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 40/56

Additional Datasource Methods

• Titles for section headers and footers

• Allow editing and reordering cells

Page 41: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 41/56

Appearance & Behavior

Page 42: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 42/56

UITableView Delegate

• Customize appearance and behavior

• Keep application logic separate from view

• Often the same object as datasource

Page 43: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 43/56

Page 44: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 44/56

Row Selection in Table Views

• In iPhone applications, rows rarely stay selected

• Selecting a row usually triggers an event

Page 45: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 45/56

Responding to Selection

// For a navigation hierarchy...

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(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.navigationControllerpushViewController:myViewController animated:YES];

}

Page 46: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 46/56

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 47: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 47/56

UITableViewController

Page 48: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 48/56

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 49: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 49/56

Page 50: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 50/56

Table View Cells

Page 51: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 51/56

Basic properties

• UITableViewCell has image and text properties

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

cell.text = @“Barack Obama”;

Page 52: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 52/56

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 *)tableaccessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark 

Page 53: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 53/56

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 54: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 54/56

Custom Row Heights

• Rows in a table view may have variable heights

• NSString category in UIStringDrawing.h is very useful forcomputing 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 55: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 55/56

Demo:

Presence - Part 2

Page 56: Standford CS 193P: 09-Table Views

8/14/2019 Standford CS 193P: 09-Table Views

http://slidepdf.com/reader/full/standford-cs-193p-09-table-views 56/56

Questions?