08 table views

174
CS193P - Lecture 8 iPhone Application Development Scroll Views & Table Views 1 Saturday, January 30, 2010

Upload: nguyen-thai

Post on 15-May-2015

682 views

Category:

Technology


0 download

DESCRIPTION

slide 8 in iphone programming course (stanford university)

TRANSCRIPT

Page 1: 08 table views

CS193P - Lecture 8iPhone Application Development

Scroll Views & Table Views

1Saturday, January 30, 2010

Page 2: 08 table views

2Saturday, January 30, 2010

Page 3: 08 table views

iPhone SDK 3.2

3Saturday, January 30, 2010

Page 4: 08 table views

iPhone SDK 3.2• Support for iPad

3Saturday, January 30, 2010

Page 5: 08 table views

iPhone SDK 3.2• Support for iPad• Beta Release

3Saturday, January 30, 2010

Page 6: 08 table views

iPhone SDK 3.2• Support for iPad• Beta Release

Enrollment in iPhone Developer Standard or Enterprise required

3Saturday, January 30, 2010

Page 7: 08 table views

Announcements• Paparazzi 1 due next Wednesday (2/3)

4Saturday, January 30, 2010

Page 8: 08 table views

Today’s Topics• Scroll views• Table views

■ Displaying data■ Controlling appearance & behavior

• UITableViewController• Table view cells

5Saturday, January 30, 2010

Page 9: 08 table views

Scroll Views

6Saturday, January 30, 2010

Page 10: 08 table views

UIScrollView• For displaying more content than can fit on the screen

7Saturday, January 30, 2010

Page 11: 08 table views

UIScrollView• For displaying more content than can fit on the screen• Handles gestures for panning and zooming

7Saturday, January 30, 2010

Page 12: 08 table views

UIScrollView• For displaying more content than can fit on the screen• Handles gestures for panning and zooming• Noteworthy subclasses: UITableView and UITextView

7Saturday, January 30, 2010

Page 13: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 14: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 15: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 16: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 17: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 18: 08 table views

Scrolling Examples

8Saturday, January 30, 2010

Page 19: 08 table views

Content Size

9Saturday, January 30, 2010

Page 20: 08 table views

Content Size

contentSize.height

contentSize.width

9Saturday, January 30, 2010

Page 21: 08 table views

Content Inset

contentSize.height

contentSize.width

contentInset.top

contentInset.bottom

10Saturday, January 30, 2010

Page 22: 08 table views

Content Inset

11Saturday, January 30, 2010

Page 23: 08 table views

Content Inset

11Saturday, January 30, 2010

Page 24: 08 table views

Content Inset

12Saturday, January 30, 2010

Page 25: 08 table views

Content Inset

contentInset.top

12Saturday, January 30, 2010

Page 26: 08 table views

Content Inset

contentInset.top

13Saturday, January 30, 2010

Page 27: 08 table views

Content Inset

contentInset.bottom

contentInset.top

13Saturday, January 30, 2010

Page 28: 08 table views

Scroll Indicator Insets

14Saturday, January 30, 2010

Page 29: 08 table views

Scroll Indicator Insets

14Saturday, January 30, 2010

Page 30: 08 table views

Scroll Indicator Insets

14Saturday, January 30, 2010

Page 31: 08 table views

scrollIndicatorInsets.top

Scroll Indicator Insets

14Saturday, January 30, 2010

Page 32: 08 table views

ContentOffset

15Saturday, January 30, 2010

Page 33: 08 table views

ContentOffset

15Saturday, January 30, 2010

Page 34: 08 table views

ContentOffset

15Saturday, January 30, 2010

Page 35: 08 table views

contentSize.height

contentSize.width

contentInset.top

contentInset.bottom

contentInset.left contentInset.right

16Saturday, January 30, 2010

Page 36: 08 table views

17Saturday, January 30, 2010

Page 37: 08 table views

contentOffset.y

contentOffset.x

17Saturday, January 30, 2010

Page 38: 08 table views

18Saturday, January 30, 2010

Page 39: 08 table views

18Saturday, January 30, 2010

Page 40: 08 table views

contentOffset.y(-contentInset.top)

contentOffset.x(-contentInset.left)

18Saturday, January 30, 2010

Page 41: 08 table views

Using a Scroll View

19Saturday, January 30, 2010

Page 42: 08 table views

Using a Scroll View• Create with the desired frame

CGRect frame = CGRectMake(0, 0, 200, 200);scrollView = [[UIScrollView alloc] initWithFrame:frame];

19Saturday, January 30, 2010

Page 43: 08 table views

Using a Scroll View• Create with the desired frame

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

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

19Saturday, January 30, 2010

Page 44: 08 table views

Using a Scroll View• Create with the desired frame

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

• Set the content sizescrollView.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];

19Saturday, January 30, 2010

Page 45: 08 table views

Extending Scroll View Behavior• Applications often want to know about scroll events

20Saturday, January 30, 2010

Page 46: 08 table views

Extending Scroll View Behavior• Applications often want to know about scroll events

■ When the scroll offset is changed

20Saturday, January 30, 2010

Page 47: 08 table views

Extending Scroll View Behavior• Applications often want to know about scroll events

■ When the scroll offset is changed■ When dragging begins & ends

20Saturday, January 30, 2010

Page 48: 08 table views

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

20Saturday, January 30, 2010

Page 49: 08 table views

Extending with a Subclass• Create a subclass• Override methods to customize behavior

21Saturday, January 30, 2010

Page 50: 08 table views

Extending with a Subclass• Create a subclass• Override methods to customize behavior

• Issues with this approach

21Saturday, January 30, 2010

Page 51: 08 table views

Extending with a Subclass• Create a subclass• Override methods to customize behavior

• Issues with this approach■ Application logic and behavior is now part of a View class

21Saturday, January 30, 2010

Page 52: 08 table views

Extending with a Subclass• Create a subclass• Override methods to customize behavior

• Issues with this approach■ Application logic and behavior is now part of a View class■ Tedious to write a one-off subclass for every scroll view instance

21Saturday, January 30, 2010

Page 53: 08 table views

Extending with a Subclass• Create a subclass• Override methods to customize behavior

• Issues with this approach■ Application logic and behavior is now part of a View class■ Tedious to write a one-off subclass for every scroll view instance■ Your code becomes tightly coupled with superclass

21Saturday, January 30, 2010

Page 54: 08 table views

Extending with Delegation• Delegate is a separate object• Clearly defined points of responsibility

■ Change behavior■ Customize appearance

• Loosely coupled with the object being extended

22Saturday, January 30, 2010

Page 55: 08 table views

UIScrollView Delegate

23Saturday, January 30, 2010

Page 56: 08 table views

UIScrollView Delegate

@protocol UIScrollViewDelegate<NSObject>

23Saturday, January 30, 2010

Page 57: 08 table views

UIScrollView Delegate

@protocol UIScrollViewDelegate<NSObject>

@optional

23Saturday, January 30, 2010

Page 58: 08 table views

UIScrollView Delegate

@protocol UIScrollViewDelegate<NSObject>

@optional

// Respond to interesting events- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

23Saturday, January 30, 2010

Page 59: 08 table views

UIScrollView Delegate

@protocol UIScrollViewDelegate<NSObject>

@optional

// Respond to interesting events- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

...

// Influence behavior- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

@end

23Saturday, January 30, 2010

Page 60: 08 table views

Implementing a Delegate

24Saturday, January 30, 2010

Page 61: 08 table views

Implementing a Delegate• Conform to the delegate protocol

@interface MyController : NSObject <UIScrollViewDelegate>

24Saturday, January 30, 2010

Page 62: 08 table views

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 positionif (scrollView.contentOffset ...) {

}}

24Saturday, January 30, 2010

Page 63: 08 table views

Zooming with a Scroll View

25Saturday, January 30, 2010

Page 64: 08 table views

Zooming with a Scroll View• Set the minimum, maximum, initial zoom scalesscrollView.maximumZoomScale = 2.0;scrollView.minimumZoomScale = scrollView.size.width / myImage.size.width;

25Saturday, January 30, 2010

Page 65: 08 table views

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

25Saturday, January 30, 2010

Page 66: 08 table views

- (void)setZoomScale:(float)scale animated:(BOOL);

Set Zoom Scale

26Saturday, January 30, 2010

Page 67: 08 table views

- (void)setZoomScale:(float)scale animated:(BOOL);

Set Zoom Scale

26Saturday, January 30, 2010

Page 68: 08 table views

- (void)setZoomScale:(float)scale animated:(BOOL);

Set Zoom Scale

26Saturday, January 30, 2010

Page 69: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

27Saturday, January 30, 2010

Page 70: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

27Saturday, January 30, 2010

Page 71: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

27Saturday, January 30, 2010

Page 72: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

28Saturday, January 30, 2010

Page 73: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

28Saturday, January 30, 2010

Page 74: 08 table views

- (void)zoomToRect:(CGRect)rect animated:(BOOL);

Zoom to Rect

28Saturday, January 30, 2010

Page 75: 08 table views

Demo

29Saturday, January 30, 2010

Page 76: 08 table views

Table Views

30Saturday, January 30, 2010

Page 77: 08 table views

Table Views• Display lists of content

■ Single column, multiple rows ■ Vertical scrolling■ Large data sets

• Powerful and ubiquitous in iPhone applications

31Saturday, January 30, 2010

Page 78: 08 table views

Table View Styles

32Saturday, January 30, 2010

Page 79: 08 table views

Table View Styles

UITableViewStylePlain

32Saturday, January 30, 2010

Page 80: 08 table views

Table View Styles

UITableViewStylePlain UITableViewStyleGrouped

32Saturday, January 30, 2010

Page 81: 08 table views

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 82: 08 table views

Table Header

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 83: 08 table views

Table Footer

Table Header

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 84: 08 table views

Table Footer

Table Header

Section

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 85: 08 table views

Table Footer

Table HeaderSection Header

Section

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 86: 08 table views

Table Footer

Table HeaderSection Header

Section Footer

Section

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 87: 08 table views

Table Cell

Table Footer

Table HeaderSection Header

Section Footer

Section

Table View AnatomyPlain Style

33Saturday, January 30, 2010

Page 88: 08 table views

Table Cell

Table Footer

Table HeaderSection Header

Section Footer

Section

Table View AnatomyPlain Style

34Saturday, January 30, 2010

Page 89: 08 table views

Table Cell

Table Footer

Table Header

Section Header

Section Footer

Section

Table View AnatomyGrouped Style

35Saturday, January 30, 2010

Page 90: 08 table views

Using Table Views• Displaying your data in the table view• Customizing appearance & behavior

36Saturday, January 30, 2010

Page 91: 08 table views

Displaying Data in a Table View

37Saturday, January 30, 2010

Page 92: 08 table views

A Naïve Solution

38Saturday, January 30, 2010

Page 93: 08 table views

A Naïve Solution• Table views display a list of data, so use an array

[myTableView setList:myListOfStuff];

38Saturday, January 30, 2010

Page 94: 08 table views

A Naïve Solution• Table views display a list of data, so use an array

[myTableView setList:myListOfStuff];

• Issues with this approach

38Saturday, January 30, 2010

Page 95: 08 table views

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

38Saturday, January 30, 2010

Page 96: 08 table views

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

38Saturday, January 30, 2010

Page 97: 08 table views

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

39Saturday, January 30, 2010

Page 98: 08 table views

UITableViewDataSource

40Saturday, January 30, 2010

Page 99: 08 table views

UITableViewDataSource• 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;

40Saturday, January 30, 2010

Page 100: 08 table views

UITableViewDataSource• 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;

40Saturday, January 30, 2010

Page 101: 08 table views

Datasource Message Flow

Datasource

41Saturday, January 30, 2010

Page 102: 08 table views

Datasource Message Flow

Datasource

How many sections?

numberOfSectionsInTableView:

41Saturday, January 30, 2010

Page 103: 08 table views

Datasource Message Flow

Datasource

numberOfSectionsInTableView:

5

41Saturday, January 30, 2010

Page 104: 08 table views

Datasource Message Flow

Datasource

42Saturday, January 30, 2010

Page 105: 08 table views

Datasource Message Flow

Datasource

tableView:numberOfRowsInSection:

How many rows in section 0?

42Saturday, January 30, 2010

Page 106: 08 table views

Datasource Message Flow

Datasource

tableView:numberOfRowsInSection:

1

42Saturday, January 30, 2010

Page 107: 08 table views

Datasource Message Flow

Datasource

43Saturday, January 30, 2010

Page 108: 08 table views

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

What to display at section 0, row 0?

43Saturday, January 30, 2010

Page 109: 08 table views

Datasource Message Flow

Datasource

tableView:cellForRowAtIndexPath:

Cell with text “John Appleseed”

43Saturday, January 30, 2010

Page 110: 08 table views

NSIndexPath• Generic class in Foundation• Path to a specific node in a tree of nested arrays

0

1

2

3

4

44Saturday, January 30, 2010

Page 111: 08 table views

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

44Saturday, January 30, 2010

Page 112: 08 table views

NSIndexPath and Table Views• Cell location described with an index path

■ Section index + row index

45Saturday, January 30, 2010

Page 113: 08 table views

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

45Saturday, January 30, 2010

Page 114: 08 table views

Single Section Table View

46Saturday, January 30, 2010

Page 115: 08 table views

Single Section Table View• Return the number of rows- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [myStrings count];}

46Saturday, January 30, 2010

Page 116: 08 table views

Single Section Table View• Return the number of rows

• Provide a cell when requested

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = ...;cell.textLabel.text = [myStrings objectAtIndex:indexPath.row]return [cell autorelease];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [myStrings count];}

46Saturday, January 30, 2010

Page 117: 08 table views

Cell Reuse

47Saturday, January 30, 2010

Page 118: 08 table views

Cell Reuse• When asked for a cell, it would be expensive to create a new

cell each time.

47Saturday, January 30, 2010

Page 119: 08 table views

Cell Reuse• When asked for a cell, it would be expensive to create a new

cell each time.

- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;

47Saturday, January 30, 2010

Page 120: 08 table views

Cell Reuse• When asked for a cell, it would be expensive to create a new

cell each time.

- (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;}

47Saturday, January 30, 2010

Page 121: 08 table views

Triggering Updates• When is the datasource asked for its data?

48Saturday, January 30, 2010

Page 122: 08 table views

Triggering Updates• When is the datasource asked for its data?

■ When a row becomes visible

48Saturday, January 30, 2010

Page 123: 08 table views

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

48Saturday, January 30, 2010

Page 124: 08 table views

Section and Row Reloading

49Saturday, January 30, 2010

Page 125: 08 table views

Section and Row Reloading- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 126: 08 table views

Section and Row Reloading

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 127: 08 table views

Section and Row Reloading

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 128: 08 table views

Section and Row Reloading

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 129: 08 table views

Section and Row Reloading

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 130: 08 table views

Section and Row Reloading

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

49Saturday, January 30, 2010

Page 131: 08 table views

Additional Datasource Methods• Titles for section headers and footers• Allow editing and reordering cells

50Saturday, January 30, 2010

Page 132: 08 table views

Appearance & Behavior

51Saturday, January 30, 2010

Page 133: 08 table views

UITableView Delegate• Customize appearance and behavior• Keep application logic separate from view

• Often the same object as datasource

52Saturday, January 30, 2010

Page 134: 08 table views

Table View Appearance & Behavior

53Saturday, January 30, 2010

Page 135: 08 table views

Table View Appearance & Behavior• Customize appearance of table view cell

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath;

53Saturday, January 30, 2010

Page 136: 08 table views

Table View Appearance & Behavior• Customize appearance of table view cell

• Validate and respond to selection changes

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath;

53Saturday, January 30, 2010

Page 137: 08 table views

Row Selection in Table Views• In iPhone applications, rows rarely stay selected• Selecting a row usually triggers an event

54Saturday, January 30, 2010

Page 138: 08 table views

Row Selection in Table Views• In iPhone applications, rows rarely stay selected• Selecting a row usually triggers an event

54Saturday, January 30, 2010

Page 139: 08 table views

Persistent Selection

55Saturday, January 30, 2010

Page 140: 08 table views

Persistent Selection

55Saturday, January 30, 2010

Page 141: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

56Saturday, January 30, 2010

Page 142: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it represents

56Saturday, January 30, 2010

Page 143: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.row

56Saturday, January 30, 2010

Page 144: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.rowid objectToDisplay = [myObjects objectAtIndex:row];

56Saturday, January 30, 2010

Page 145: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.rowid objectToDisplay = [myObjects objectAtIndex:row];

// Create a new view controller and pass it along

56Saturday, January 30, 2010

Page 146: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.rowid objectToDisplay = [myObjects objectAtIndex:row];

// Create a new view controller and pass it alongMyViewController *myViewController = ...;

56Saturday, January 30, 2010

Page 147: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.rowid objectToDisplay = [myObjects objectAtIndex:row];

// Create a new view controller and pass it alongMyViewController *myViewController = ...;myViewController.object = objectToDisplay;

56Saturday, January 30, 2010

Page 148: 08 table views

Responding to Selection// For a navigation hierarchy...- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Get the row and the object it representsNSUInteger row = indexPath.rowid objectToDisplay = [myObjects objectAtIndex:row];

// Create a new view controller and pass it alongMyViewController *myViewController = ...;myViewController.object = objectToDisplay;

[self.navigationController pushViewController:myViewController animated:YES];

}

56Saturday, January 30, 2010

Page 149: 08 table views

Altering or Disabling Selection- (NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// Don’t allow selecting certain rows?if (indexPath.row == ...) {

return nil;} else {

return indexPath;}

}

57Saturday, January 30, 2010

Page 150: 08 table views

UITableViewController

58Saturday, January 30, 2010

Page 151: 08 table views

UITableViewController

59Saturday, January 30, 2010

Page 152: 08 table views

UITableViewController• Convenient starting point for view controller with a table view

■ Table view is automatically created■ Controller is table view’s delegate and datasource

59Saturday, January 30, 2010

Page 153: 08 table views

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

59Saturday, January 30, 2010

Page 154: 08 table views

Table View Cells

60Saturday, January 30, 2010

Page 155: 08 table views

Designated Initializer

61Saturday, January 30, 2010

Page 156: 08 table views

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier;

Designated Initializer

61Saturday, January 30, 2010

Page 157: 08 table views

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier;

Designated Initializer

DEPRECATED

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

61Saturday, January 30, 2010

Page 158: 08 table views

Cell Styles

62Saturday, January 30, 2010

Page 159: 08 table views

Cell Styles

UITableViewCellStyleDefault

62Saturday, January 30, 2010

Page 160: 08 table views

Cell Styles

UITableViewCellStyleDefault

UITableViewCellStyleSubtitle

62Saturday, January 30, 2010

Page 161: 08 table views

Cell Styles

UITableViewCellStyleDefault

UITableViewCellStyleSubtitle

UITableViewCellStyleValue1

62Saturday, January 30, 2010

Page 162: 08 table views

Cell Styles

UITableViewCellStyleDefault

UITableViewCellStyleSubtitle

UITableViewCellStyleValue1

UITableViewCellStyleValue2

62Saturday, January 30, 2010

Page 163: 08 table views

Basic properties• UITableViewCell has an image view and one or two text labels

63Saturday, January 30, 2010

Page 164: 08 table views

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

63Saturday, January 30, 2010

Page 165: 08 table views

Accessory Types// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

64Saturday, January 30, 2010

Page 166: 08 table views

Accessory Types// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

64Saturday, January 30, 2010

Page 167: 08 table views

Accessory Types// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

64Saturday, January 30, 2010

Page 168: 08 table views

Accessory Types

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

// Only for the blue disclosure buttonNSUInteger row = indexPath.row;...

}

// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

64Saturday, January 30, 2010

Page 169: 08 table views

Accessory Types

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

// Only for the blue disclosure buttonNSUInteger row = indexPath.row;...

}

// UITableView delegate method- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

64Saturday, January 30, 2010

Page 170: 08 table views

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

65Saturday, January 30, 2010

Page 171: 08 table views

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

65Saturday, January 30, 2010

Page 172: 08 table views

66Saturday, January 30, 2010

Page 173: 08 table views

66Saturday, January 30, 2010

Page 174: 08 table views

Questions?

67Saturday, January 30, 2010