Download - Lecture 8 Slides (April 27, 2009)
![Page 1: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/1.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 1/64
CS193P - Lecture 8
iPhone Application Development
Scroll Views & Table Views
![Page 2: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/2.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 2/64
![Page 3: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/3.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 3/64
Announcements
• Enrolled students who requested iPod touches can pick themup after class today! Need Student ID
! No grade if not returned!
![Page 4: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/4.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 4/64
Today’s Topics
• Scroll views
• Table views! Displaying data
! Controlling appearance & behavior
• UITableViewController• Table view cells
• Presence - Part 2
![Page 5: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/5.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 5/64
Scroll Views
![Page 6: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/6.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 6/64
UIScrollView
• For displaying more content than can fit on the screen
• Handles gestures for panning and zooming
• Noteworthy subclasses: UITableView and UITextView
![Page 7: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/7.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 7/64
Scrolling Examples
![Page 8: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/8.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 8/64
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 9: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/9.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 9/64
Frame and Content
scrollView.frame
![Page 10: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/10.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 10/64
![Page 11: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/11.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 11/64
Frame and Content
scrollView.contentOffset
![Page 12: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/12.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 12/64
Demo:
Using a UIScrollView
![Page 13: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/13.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 13/64
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 14: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/14.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 14/64
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
![Page 15: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/15.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 15/64
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 16: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/16.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 16/64
![Page 17: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/17.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 17/64
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 18: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/18.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 18/64
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 19: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/19.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 19/64
Demo:
Zooming with a UIScrollView
![Page 20: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/20.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 20/64
Table Views
![Page 21: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/21.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 21/64
Table Views
• Display lists of content! Single column, multiple rows
! Vertical scrolling
! Large data sets
• Powerful and ubiquitous in iPhone applications
![Page 22: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/22.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 22/64
Table View Styles
UITableViewStylePlain UITableViewStyleGrouped
![Page 23: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/23.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 23/64
Table Header
Table View AnatomyPlain Style
![Page 24: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/24.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 24/64
Table Footer
Table View AnatomyPlain Style
![Page 25: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/25.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 25/64
Section
Table View AnatomyPlain Style
![Page 26: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/26.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 26/64
![Page 27: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/27.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 27/64
Section Footer
Table View AnatomyPlain Style
![Page 28: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/28.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 28/64
![Page 29: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/29.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 29/64
![Page 30: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/30.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 30/64
Table Cell
Table Footer
Table Header
Section Header
Section Footer
Section
Table View AnatomyGrouped Style
![Page 31: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/31.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 31/64
![Page 32: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/32.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 32/64
Displaying Data in a Table View
![Page 33: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/33.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 33/64
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 34: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/34.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 34/64
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 35: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/35.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 35/64
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;
![Page 36: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/36.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 36/64
![Page 37: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/37.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 37/64
![Page 38: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/38.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 38/64
Datasource Message Flow
Datasource
tableView:numberOfRowsInSection:
How many rows
in section 0?
![Page 39: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/39.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 39/64
Datasource Message Flow
Datasource
tableView:numberOfRowsInSection:
1
![Page 40: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/40.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 40/64
Datasource Message Flow
Datasource
tableView:cellForRowAtIndexPath:
What to display at
section 0, row 0?
![Page 41: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/41.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 41/64
Datasource Message Flow
Datasource
tableView:cellForRowAtIndexPath:
Cell with text“John Appleseed”
![Page 42: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/42.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 42/64
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 43: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/43.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 43/64
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 44: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/44.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 44/64
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 45: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/45.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 45/64
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]
initWithFrame:... reuseIdentifier:@”MyIdenifier”];
}
cell.text = [myStrings objectAtIndex:indexPath.row]
return cell;
}
![Page 46: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/46.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 46/64
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 47: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/47.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 47/64
Additional Datasource Methods
• Titles for section headers and footers
• Allow editing and reordering cells
![Page 48: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/48.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 48/64
Appearance & Behavior
![Page 49: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/49.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 49/64
UITableView Delegate
• Customize appearance and behavior
• Keep application logic separate from view
• Often the same object as datasource
![Page 50: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/50.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 50/64
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 51: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/51.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 51/64
Row Selection in Table Views
• In iPhone applications, rows rarely stay selected
• Selecting a row usually triggers an event
![Page 52: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/52.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 52/64
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 53: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/53.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 53/64
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 54: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/54.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 54/64
UITableViewController
![Page 55: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/55.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 55/64
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 56: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/56.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 56/64
Demo:
UITableViewController
![Page 57: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/57.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 57/64
Table View Cells
![Page 58: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/58.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 58/64
Basic properties
• UITableViewCell has image and text properties
cell.image = [UIImage imageNamed:@“obama.png”];
cell.text = @“Barack Obama”;
![Page 59: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/59.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 59/64
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 60: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/60.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 60/64
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];
}
![Page 61: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/61.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 61/64
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 62: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/62.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 62/64
Questions?
![Page 63: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/63.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 63/64
Presence 2
• Due next Tuesday 5/5 at 11:59PM! Displaying dynamic data with table views! Fetching data over the Internet using web services
![Page 64: Lecture 8 Slides (April 27, 2009)](https://reader031.vdocument.in/reader031/viewer/2022021215/577d340b1a28ab3a6b8c7d44/html5/thumbnails/64.jpg)
8/8/2019 Lecture 8 Slides (April 27, 2009)
http://slidepdf.com/reader/full/lecture-8-slides-april-27-2009 64/64
Demo:
Presence - Part 2