SlideShare a Scribd company logo
1 of 174
Download to read offline
CS193P - Lecture 8
        iPhone Application Development

        Scroll Views & Table Views




Saturday, January 30, 2010               1
Saturday, January 30, 2010   2
iPhone SDK 3.2




Saturday, January 30, 2010   3
iPhone SDK 3.2
       • Support for iPad




Saturday, January 30, 2010   3
iPhone SDK 3.2
       • Support for iPad
       • Beta Release




Saturday, January 30, 2010   3
iPhone SDK 3.2
       • Support for iPad
       • Beta Release




              Enrollment in iPhone Developer Standard or Enterprise required




Saturday, January 30, 2010                                                     3
Announcements
        • Paparazzi 1 due next Wednesday (2/3)




Saturday, January 30, 2010                       4
Today’s Topics
        • Scroll views
        • Table views
             ■ Displaying data
             ■ Controlling appearance & behavior


        • UITableViewController
        • Table view cells




Saturday, January 30, 2010                         5
Scroll Views




Saturday, January 30, 2010   6
UIScrollView
        • For displaying more content than can fit on the screen




Saturday, January 30, 2010                                         7
UIScrollView
        • For displaying more content than can fit on the screen
        • Handles gestures for panning and zooming




Saturday, January 30, 2010                                         7
UIScrollView
        • For displaying more content than can fit on the screen
        • Handles gestures for panning and zooming
        • Noteworthy subclasses: UITableView and UITextView




Saturday, January 30, 2010                                         7
Scrolling Examples




Saturday, January 30, 2010                        8
Scrolling Examples




Saturday, January 30, 2010                        8
Scrolling Examples




Saturday, January 30, 2010                        8
Scrolling Examples




Saturday, January 30, 2010                        8
Scrolling Examples




Saturday, January 30, 2010                        8
Scrolling Examples




Saturday, January 30, 2010                        8
Content Size




Saturday, January 30, 2010   9
Content Size
                                       contentSize.width




                  contentSize.height




Saturday, January 30, 2010                                 9
Content Inset                   contentSize.width

                                                           contentInset.top




                  contentSize.height




                                                           contentInset.bottom




Saturday, January 30, 2010                                                       10
Content Inset




Saturday, January 30, 2010   11
Content Inset




Saturday, January 30, 2010   11
Content Inset




Saturday, January 30, 2010   12
Content Inset

              contentInset.top




Saturday, January 30, 2010       12
Content Inset

              contentInset.top




Saturday, January 30, 2010       13
Content Inset

              contentInset.top




        contentInset.bottom




Saturday, January 30, 2010       13
Scroll Indicator Insets




Saturday, January 30, 2010       14
Scroll Indicator Insets




Saturday, January 30, 2010       14
Scroll Indicator Insets




Saturday, January 30, 2010       14
Scroll Indicator Insets

    scrollIndicatorInsets.top




Saturday, January 30, 2010       14
Content
       Offset




Saturday, January 30, 2010   15
Content
       Offset




Saturday, January 30, 2010   15
Content
       Offset




Saturday, January 30, 2010   15
contentSize.width
                                                                contentInset.top




         contentSize.height




                                                                contentInset.bottom
                             contentInset.left       contentInset.right



Saturday, January 30, 2010                                                            16
Saturday, January 30, 2010   17
contentOffset.x

               contentOffset.y




Saturday, January 30, 2010                     17
Saturday, January 30, 2010   18
Saturday, January 30, 2010   18
contentOffset.x
                      (-contentInset.left)
           contentOffset.y
         (-contentInset.top)




Saturday, January 30, 2010                   18
Using a Scroll View




Saturday, January 30, 2010    19
Using a Scroll View
        • Create with the desired frame
             CGRect frame = CGRectMake(0, 0, 200, 200);
             scrollView = [[UIScrollView alloc] initWithFrame:frame];




Saturday, January 30, 2010                                              19
Using a Scroll View
        • Create with the desired frame
             CGRect frame = CGRectMake(0, 0, 200, 200);
             scrollView = [[UIScrollView alloc] initWithFrame:frame];


        • Add subviews (frames may extend beyond scroll view bounds)
             frame = CGRectMake(0, 0, 500, 500);
             myImageView = [[UIImageView alloc] initWithFrame:frame];
             [scrollView addSubview:myImageView];




Saturday, January 30, 2010                                              19
Using a Scroll View
        • Create with the desired frame
             CGRect frame = CGRectMake(0, 0, 200, 200);
             scrollView = [[UIScrollView alloc] initWithFrame:frame];


        • Add subviews (frames may extend beyond scroll view bounds)
             frame = CGRectMake(0, 0, 500, 500);
             myImageView = [[UIImageView alloc] initWithFrame:frame];
             [scrollView addSubview:myImageView];


        • Set the content size
             scrollView.contentSize = CGSizeMake(500, 500);




Saturday, January 30, 2010                                              19
Extending Scroll View Behavior
        • Applications often want to know about scroll events




Saturday, January 30, 2010                                      20
Extending Scroll View Behavior
        • Applications often want to know about scroll events
             ■   When the scroll offset is changed




Saturday, January 30, 2010                                      20
Extending Scroll View Behavior
        • Applications often want to know about scroll events
             ■ When the scroll offset is changed
             ■ When dragging begins & ends




Saturday, January 30, 2010                                      20
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




Saturday, January 30, 2010                                      20
Extending with a Subclass
        • Create a subclass
        • Override methods to customize behavior




Saturday, January 30, 2010                         21
Extending with a Subclass
        • Create a subclass
        • Override methods to customize behavior
        • Issues with this approach




Saturday, January 30, 2010                         21
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




Saturday, January 30, 2010                                                    21
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




Saturday, January 30, 2010                                                          21
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




Saturday, January 30, 2010                                                          21
Extending with Delegation
        • Delegate is a separate object
        • Clearly defined points of responsibility
             ■ Change behavior
             ■ Customize appearance


        • Loosely coupled with the object being extended




Saturday, January 30, 2010                                 22
UIScrollView Delegate




Saturday, January 30, 2010      23
UIScrollView Delegate
          @protocol UIScrollViewDelegate<NSObject>




Saturday, January 30, 2010                           23
UIScrollView Delegate
          @protocol UIScrollViewDelegate<NSObject>

          @optional




Saturday, January 30, 2010                           23
UIScrollView Delegate
          @protocol UIScrollViewDelegate<NSObject>

          @optional

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




Saturday, January 30, 2010                                          23
UIScrollView Delegate
          @protocol UIScrollViewDelegate<NSObject>

          @optional

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

          ...

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

          @end




Saturday, January 30, 2010                                                  23
Implementing a Delegate




Saturday, January 30, 2010        24
Implementing a Delegate
        • Conform to the delegate protocol
            @interface MyController : NSObject <UIScrollViewDelegate>




Saturday, January 30, 2010                                              24
Implementing a Delegate
        • Conform to the delegate protocol
            @interface MyController : NSObject <UIScrollViewDelegate>

        • Implement all required methods and any optional methods
            - (void)scrollViewDidScroll:(UIScrollView *)scrollView
            {
              // Do something in response to the new scroll position
              if (scrollView.contentOffset ...) {

                }
            }




Saturday, January 30, 2010                                              24
Zooming with a Scroll View




Saturday, January 30, 2010           25
Zooming with a Scroll View
        • Set the minimum, maximum, initial zoom scales
            scrollView.maximumZoomScale = 2.0;
            scrollView.minimumZoomScale = scrollView.size.width /
                                          myImage.size.width;




Saturday, January 30, 2010                                          25
Zooming with a Scroll View
        • Set the minimum, maximum, initial zoom scales
            scrollView.maximumZoomScale = 2.0;
            scrollView.minimumZoomScale = scrollView.size.width /
                                          myImage.size.width;


        • Implement delegate method for zooming
            - (UIView *)viewForZoomingInScrollView:(UIView *)view
            {
              return someViewThatWillBeScaled;
            }




Saturday, January 30, 2010                                          25
Set Zoom Scale




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




Saturday, January 30, 2010                                                        26
Set Zoom Scale




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




Saturday, January 30, 2010                                                        26
Set Zoom Scale




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




Saturday, January 30, 2010                                                        26
Zoom to Rect




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




Saturday, January 30, 2010                                                      27
Zoom to Rect




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




Saturday, January 30, 2010                                                      27
Zoom to Rect




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




Saturday, January 30, 2010                                                      27
Zoom to Rect




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




Saturday, January 30, 2010                                                      28
Zoom to Rect




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




Saturday, January 30, 2010                                                      28
Zoom to Rect




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




Saturday, January 30, 2010                                                      28
Demo




Saturday, January 30, 2010   29
Table Views




Saturday, January 30, 2010   30
Table Views
        • Display lists of content
             ■ Single column, multiple rows
             ■ Vertical scrolling

             ■ Large data sets


        • Powerful and ubiquitous in iPhone applications




Saturday, January 30, 2010                                 31
Table View Styles




Saturday, January 30, 2010   32
Table View Styles
                    UITableViewStylePlain




Saturday, January 30, 2010                  32
Table View Styles
                    UITableViewStylePlain   UITableViewStyleGrouped




Saturday, January 30, 2010                                            32
Table View Anatomy
                                  Plain Style




Saturday, January 30, 2010                        33
Table View Anatomy
                                    Plain Style

                Table Header




Saturday, January 30, 2010                          33
Table View Anatomy
                                     Plain Style

                Table Header




                 Table Footer




Saturday, January 30, 2010                           33
Table View Anatomy
                                      Plain Style

                Table Header




                       Section


                 Table Footer




Saturday, January 30, 2010                            33
Table View Anatomy
                                      Plain Style

                Table Header
                                                      Section Header




                       Section


                 Table Footer




Saturday, January 30, 2010                                             33
Table View Anatomy
                                      Plain Style

                Table Header
                                                      Section Header



                                                      Section Footer


                       Section


                 Table Footer




Saturday, January 30, 2010                                             33
Table View Anatomy
                                      Plain Style

                Table Header
                                                      Section Header


                                                      Table Cell
                                                      Section Footer


                       Section


                 Table Footer




Saturday, January 30, 2010                                             33
Table View Anatomy
                                      Plain Style

                Table Header
                                                      Section Header


                                                      Table Cell
                                                      Section Footer


                       Section


                 Table Footer




Saturday, January 30, 2010                                             34
Table View Anatomy
                                     Grouped Style

                Table Header
                                                      Section Header


                                                      Table Cell
                                                      Section Footer



                       Section



                 Table Footer




Saturday, January 30, 2010                                             35
Using Table Views
        • Displaying your data in the table view
        • Customizing appearance & behavior




Saturday, January 30, 2010                         36
Displaying Data in a Table View




Saturday, January 30, 2010                37
A Naïve Solution




Saturday, January 30, 2010   38
A Naïve Solution
        • Table views display a list of data, so use an array
             [myTableView setList:myListOfStuff];




Saturday, January 30, 2010                                      38
A Naïve Solution
        • Table views display a list of data, so use an array
             [myTableView setList:myListOfStuff];

        • Issues with this approach




Saturday, January 30, 2010                                      38
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




Saturday, January 30, 2010                                      38
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




Saturday, January 30, 2010                                      38
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




Saturday, January 30, 2010                                 39
UITableViewDataSource




Saturday, January 30, 2010      40
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;




Saturday, January 30, 2010                                                  40
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;



        • Provide cells for table view as needed
           // Required method
           - (UITableViewCell *)tableView:(UITableView *)tableView
                    cellForRowAtIndexPath:(NSIndexPath *)indexPath;




Saturday, January 30, 2010                                                  40
Datasource Message Flow




                                                  Datasource




Saturday, January 30, 2010                                     41
Datasource Message Flow
                                    numberOfSectionsInTableView:




                                                 How many
                                                  sections?

                                                       Datasource




Saturday, January 30, 2010                                          41
Datasource Message Flow
                                      numberOfSectionsInTableView:




                                                       Datasource

                                  5




Saturday, January 30, 2010                                           41
Datasource Message Flow




                                                  Datasource




Saturday, January 30, 2010                                     42
Datasource Message Flow
                                   tableView:numberOfRowsInSection:




                                                  How many rows
                                                   in section 0?

                                                       Datasource




Saturday, January 30, 2010                                          42
Datasource Message Flow
                                      tableView:numberOfRowsInSection:




                                                         Datasource

                                  1




Saturday, January 30, 2010                                            42
Datasource Message Flow




                                                  Datasource




Saturday, January 30, 2010                                     43
Datasource Message Flow
                                   tableView:cellForRowAtIndexPath:




                                               What to display at
                                               section 0, row 0?

                                                         Datasource




Saturday, January 30, 2010                                            43
Datasource Message Flow
                                      tableView:cellForRowAtIndexPath:




                                                         Datasource

                                Cell with text
                              “John Appleseed”




Saturday, January 30, 2010                                            43
NSIndexPath
        • Generic class in Foundation
        • Path to a specific node in a tree of nested arrays

                             0
                             1
                             2
                             3
                             4




Saturday, January 30, 2010                                     44
NSIndexPath
        • Generic class in Foundation
        • Path to a specific node in a tree of nested arrays

                             0   0             0               0
                             1   1             1               1
                             2   2             2               2
                             3   3             3               3
                             4   4             4               4




Saturday, January 30, 2010                                         44
NSIndexPath and Table Views
        • Cell location described with an index path
             ■   Section index + row index




Saturday, January 30, 2010                             45
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




Saturday, January 30, 2010                                          45
Single Section Table View




Saturday, January 30, 2010          46
Single Section Table View
        • Return the number of rows
           - (NSInteger)tableView:(UITableView *)tableView
             numberOfRowsInSection:(NSInteger)section
           {
              return [myStrings count];
           }




Saturday, January 30, 2010                                   46
Single Section Table View
        • Return the number of rows
           - (NSInteger)tableView:(UITableView *)tableView
             numberOfRowsInSection:(NSInteger)section
           {
              return [myStrings count];
           }


        • Provide a cell when requested
           - (UITableViewCell *)tableView:(UITableView *)tableView
                    cellForRowAtIndexPath:(NSIndexPath *)indexPath
           {
             UITableViewCell *cell = ...;
             cell.textLabel.text = [myStrings objectAtIndex:indexPath.row]
             return [cell autorelease];
           }



Saturday, January 30, 2010                                               46
Cell Reuse




Saturday, January 30, 2010   47
Cell Reuse
        • When asked for a cell, it would be expensive to create a new
          cell each time.




Saturday, January 30, 2010                                               47
Cell Reuse
        • When asked for a cell, it would be expensive to create a new
          cell each time.
            - (UITableViewCell *)dequeueReusableCellWithIdentifier:
            (NSString *)identifier;




Saturday, January 30, 2010                                               47
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;
            }
Saturday, January 30, 2010                                               47
Triggering Updates
        • When is the datasource asked for its data?




Saturday, January 30, 2010                             48
Triggering Updates
        • When is the datasource asked for its data?
             ■   When a row becomes visible




Saturday, January 30, 2010                             48
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];
                 }




Saturday, January 30, 2010                                                     48
Section and Row Reloading




Saturday, January 30, 2010         49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;




Saturday, January 30, 2010                                          49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;

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




Saturday, January 30, 2010                                          49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;

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




Saturday, January 30, 2010                                          49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;

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



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




Saturday, January 30, 2010                                          49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;

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



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




Saturday, January 30, 2010                                          49
Section and Row Reloading
       - (void)insertSections:(NSIndexSet *)sections
             withRowAnimation:(UITableViewRowAnimation)animation;

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



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

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




Saturday, January 30, 2010                                                  49
Additional Datasource Methods
        • Titles for section headers and footers
        • Allow editing and reordering cells




Saturday, January 30, 2010                         50
Appearance & Behavior




Saturday, January 30, 2010      51
UITableView Delegate
        • Customize appearance and behavior
        • Keep application logic separate from view
        • Often the same object as datasource




Saturday, January 30, 2010                            52
Table View Appearance & Behavior




Saturday, January 30, 2010                 53
Table View Appearance & Behavior
        • Customize appearance of table view cell
            - (void)tableView:(UITableView *)tableView
              willDisplayCell:(UITableViewCell *)cell
            forRowAtIndexPath:(NSIndexPath *)indexPath;




Saturday, January 30, 2010                                53
Table View Appearance & Behavior
        • Customize appearance of table view cell
            - (void)tableView:(UITableView *)tableView
              willDisplayCell:(UITableViewCell *)cell
            forRowAtIndexPath:(NSIndexPath *)indexPath;


        • Validate and respond to selection changes
             - (NSIndexPath *)tableView:(UITableView *)tableView
             willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

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




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




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




Saturday, January 30, 2010                                    54
Persistent Selection




Saturday, January 30, 2010    55
Persistent Selection




Saturday, January 30, 2010    55
Responding to Selection
            // For a navigation hierarchy...
            - (void)tableView:(UITableView *)tableView
            didSelectRowAtIndexPath:(NSIndexPath *)indexPath
            {




Saturday, January 30, 2010                                     56
Responding to Selection
            // For a navigation hierarchy...
            - (void)tableView:(UITableView *)tableView
            didSelectRowAtIndexPath:(NSIndexPath *)indexPath
            {
              // Get the row and the object it represents




Saturday, January 30, 2010                                     56
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




Saturday, January 30, 2010                                     56
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];




Saturday, January 30, 2010                                          56
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




Saturday, January 30, 2010                                          56
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 = ...;




Saturday, January 30, 2010                                          56
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;




Saturday, January 30, 2010                                          56
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];
            }




Saturday, January 30, 2010                                            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;
              }
            }




Saturday, January 30, 2010                                        57
UITableViewController




Saturday, January 30, 2010      58
UITableViewController




Saturday, January 30, 2010      59
UITableViewController
        • Convenient starting point for view controller with a table view
             ■ Table view is automatically created
             ■ Controller is table view’s delegate and datasource




Saturday, January 30, 2010                                                  59
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




Saturday, January 30, 2010                                                  59
Table View Cells




Saturday, January 30, 2010   60
Designated Initializer




Saturday, January 30, 2010      61
Designated Initializer
       - (id)initWithFrame:(CGRect)frame
           reuseIdentifier:(NSString *)reuseIdentifier;




Saturday, January 30, 2010                                61
Designated Initializer

                ECATED
            EPR
       - (id)initWithFrame:(CGRect)frame


          DreuseIdentifier:(NSString *)reuseIdentifier;




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




Saturday, January 30, 2010                                61
Cell Styles




Saturday, January 30, 2010   62
Cell Styles


                 UITableViewCellStyleDefault




Saturday, January 30, 2010                     62
Cell Styles


                 UITableViewCellStyleDefault

                 UITableViewCellStyleSubtitle




Saturday, January 30, 2010                      62
Cell Styles


                 UITableViewCellStyleDefault

                 UITableViewCellStyleSubtitle




                 UITableViewCellStyleValue1




Saturday, January 30, 2010                      62
Cell Styles


                 UITableViewCellStyleDefault

                 UITableViewCellStyleSubtitle




                 UITableViewCellStyleValue1

                 UITableViewCellStyleValue2




Saturday, January 30, 2010                      62
Basic properties
        • UITableViewCell has an image view and one or two text labels




Saturday, January 30, 2010                                               63
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”;




Saturday, January 30, 2010                                                  63
Accessory Types
        // UITableView delegate method
        - (UITableViewCellAccessoryType)tableView:(UITableView *)table
        accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;




Saturday, January 30, 2010                                               64
Accessory Types
        // UITableView delegate method
        - (UITableViewCellAccessoryType)tableView:(UITableView *)table
        accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

          UITableViewCellAccessoryDisclosureIndicator




Saturday, January 30, 2010                                               64
Accessory Types
        // UITableView delegate method
        - (UITableViewCellAccessoryType)tableView:(UITableView *)table
        accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

          UITableViewCellAccessoryDisclosureIndicator

          UITableViewCellAccessoryDetailDisclosureButton




Saturday, January 30, 2010                                               64
Accessory Types
        // UITableView delegate method
        - (UITableViewCellAccessoryType)tableView:(UITableView *)table
        accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

          UITableViewCellAccessoryDisclosureIndicator

          UITableViewCellAccessoryDetailDisclosureButton




        - (void)tableView:(UITableView *)tableView
        accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
        {
          // Only for the blue disclosure button
          NSUInteger row = indexPath.row;
          ...
        }

Saturday, January 30, 2010                                               64
Accessory Types
        // UITableView delegate method
        - (UITableViewCellAccessoryType)tableView:(UITableView *)table
        accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath;

          UITableViewCellAccessoryDisclosureIndicator

          UITableViewCellAccessoryDetailDisclosureButton

          UITableViewCellAccessoryCheckmark

        - (void)tableView:(UITableView *)tableView
        accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
        {
          // Only for the blue disclosure button
          NSUInteger row = indexPath.row;
          ...
        }

Saturday, January 30, 2010                                               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




Saturday, January 30, 2010                                             65
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];
                 }



Saturday, January 30, 2010                                                       65
Saturday, January 30, 2010   66
Saturday, January 30, 2010   66
Questions?




Saturday, January 30, 2010   67

More Related Content

Similar to iPhone App Development Scroll & Table Views

Developing in HTML5: Widgetbox & Sencha Touch
Developing in HTML5: Widgetbox & Sencha TouchDeveloping in HTML5: Widgetbox & Sencha Touch
Developing in HTML5: Widgetbox & Sencha TouchSencha
 
The IT Philharmonic - OSCON 2010
The IT Philharmonic - OSCON 2010 The IT Philharmonic - OSCON 2010
The IT Philharmonic - OSCON 2010 Chef Software, Inc.
 
Designing With Type :: FontConf 2010
Designing With Type :: FontConf 2010Designing With Type :: FontConf 2010
Designing With Type :: FontConf 2010Kyle Meyer
 
Joomla PLT Summit Feedback
Joomla PLT Summit FeedbackJoomla PLT Summit Feedback
Joomla PLT Summit FeedbackChris Davenport
 
Why We Love Metadata (Edinburgh)
Why We Love Metadata (Edinburgh)Why We Love Metadata (Edinburgh)
Why We Love Metadata (Edinburgh)Chris Jackson
 
SenchaCon 2010 Keynote by CEO Abe Elias
SenchaCon 2010 Keynote by CEO Abe EliasSenchaCon 2010 Keynote by CEO Abe Elias
SenchaCon 2010 Keynote by CEO Abe EliasSencha
 
HTML5/CSS3 @ Baidu
HTML5/CSS3 @ BaiduHTML5/CSS3 @ Baidu
HTML5/CSS3 @ BaiduZi Bin Cheah
 
Drupal security - Configuration and process
Drupal security - Configuration and processDrupal security - Configuration and process
Drupal security - Configuration and processGábor Hojtsy
 
HTML 5: The Future of the Web
HTML 5: The Future of the WebHTML 5: The Future of the Web
HTML 5: The Future of the WebTim Wright
 
20100627 sobe2
20100627 sobe220100627 sobe2
20100627 sobe2Shu Shimbo
 
Cassandra devoxx 2010
Cassandra devoxx 2010Cassandra devoxx 2010
Cassandra devoxx 2010jbellis
 
Responsive web design - Drupal theming
Responsive web design - Drupal themingResponsive web design - Drupal theming
Responsive web design - Drupal themingadifferentdesign
 
Motorola presentation software developers
Motorola presentation software developersMotorola presentation software developers
Motorola presentation software developersVincent Everts
 

Similar to iPhone App Development Scroll & Table Views (16)

Continuous Integration
Continuous IntegrationContinuous Integration
Continuous Integration
 
Developing in HTML5: Widgetbox & Sencha Touch
Developing in HTML5: Widgetbox & Sencha TouchDeveloping in HTML5: Widgetbox & Sencha Touch
Developing in HTML5: Widgetbox & Sencha Touch
 
The IT Philharmonic - OSCON 2010
The IT Philharmonic - OSCON 2010 The IT Philharmonic - OSCON 2010
The IT Philharmonic - OSCON 2010
 
Designing With Type :: FontConf 2010
Designing With Type :: FontConf 2010Designing With Type :: FontConf 2010
Designing With Type :: FontConf 2010
 
CSS3 now
CSS3 nowCSS3 now
CSS3 now
 
Joomla PLT Summit Feedback
Joomla PLT Summit FeedbackJoomla PLT Summit Feedback
Joomla PLT Summit Feedback
 
Why We Love Metadata (Edinburgh)
Why We Love Metadata (Edinburgh)Why We Love Metadata (Edinburgh)
Why We Love Metadata (Edinburgh)
 
SenchaCon 2010 Keynote by CEO Abe Elias
SenchaCon 2010 Keynote by CEO Abe EliasSenchaCon 2010 Keynote by CEO Abe Elias
SenchaCon 2010 Keynote by CEO Abe Elias
 
Learning In the Participation Age
Learning In the Participation AgeLearning In the Participation Age
Learning In the Participation Age
 
HTML5/CSS3 @ Baidu
HTML5/CSS3 @ BaiduHTML5/CSS3 @ Baidu
HTML5/CSS3 @ Baidu
 
Drupal security - Configuration and process
Drupal security - Configuration and processDrupal security - Configuration and process
Drupal security - Configuration and process
 
HTML 5: The Future of the Web
HTML 5: The Future of the WebHTML 5: The Future of the Web
HTML 5: The Future of the Web
 
20100627 sobe2
20100627 sobe220100627 sobe2
20100627 sobe2
 
Cassandra devoxx 2010
Cassandra devoxx 2010Cassandra devoxx 2010
Cassandra devoxx 2010
 
Responsive web design - Drupal theming
Responsive web design - Drupal themingResponsive web design - Drupal theming
Responsive web design - Drupal theming
 
Motorola presentation software developers
Motorola presentation software developersMotorola presentation software developers
Motorola presentation software developers
 

More from Mahmoud

مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟Mahmoud
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتكMahmoud
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغيرMahmoud
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمMahmoud
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائقMahmoud
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقريةMahmoud
 
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهمهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهMahmoud
 
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟Mahmoud
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغيرMahmoud
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائقMahmoud
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتكMahmoud
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمMahmoud
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقريةMahmoud
 
Accident Investigation
Accident InvestigationAccident Investigation
Accident InvestigationMahmoud
 
Investigation Skills
Investigation SkillsInvestigation Skills
Investigation SkillsMahmoud
 
Building Papers
Building PapersBuilding Papers
Building PapersMahmoud
 
Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Mahmoud
 
Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Mahmoud
 
A Basic Modern Russian Grammar
A Basic Modern Russian Grammar A Basic Modern Russian Grammar
A Basic Modern Russian Grammar Mahmoud
 

More from Mahmoud (20)

مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتك
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغير
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقرية
 
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيهمهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
مهارات كتابه السيرة الذاتيه واجتياز المقابله الشخصيه
 
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
مهارات التفكير الإبتكاري  كيف تكون مبدعا؟مهارات التفكير الإبتكاري  كيف تكون مبدعا؟
مهارات التفكير الإبتكاري كيف تكون مبدعا؟
 
مهارات التعامل مع الغير
مهارات التعامل مع الغيرمهارات التعامل مع الغير
مهارات التعامل مع الغير
 
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائقتطوير الذاكرة    تعلم كيف تحفظ 56 كلمة كل 10 دقائق
تطوير الذاكرة تعلم كيف تحفظ 56 كلمة كل 10 دقائق
 
كيف تقوى ذاكرتك
كيف تقوى ذاكرتككيف تقوى ذاكرتك
كيف تقوى ذاكرتك
 
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكمستيفن كوفي ( ادارة الاولويات ) لايفوتكم
ستيفن كوفي ( ادارة الاولويات ) لايفوتكم
 
الشخصية العبقرية
الشخصية العبقريةالشخصية العبقرية
الشخصية العبقرية
 
Accident Investigation
Accident InvestigationAccident Investigation
Accident Investigation
 
Investigation Skills
Investigation SkillsInvestigation Skills
Investigation Skills
 
Building Papers
Building PapersBuilding Papers
Building Papers
 
Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02Appleipad 100205071918 Phpapp02
Appleipad 100205071918 Phpapp02
 
Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01Operatingsystemwars 100209023952 Phpapp01
Operatingsystemwars 100209023952 Phpapp01
 
A Basic Modern Russian Grammar
A Basic Modern Russian Grammar A Basic Modern Russian Grammar
A Basic Modern Russian Grammar
 
Teams Ar
Teams ArTeams Ar
Teams Ar
 

Recently uploaded

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

iPhone App Development Scroll & Table Views

  • 1. CS193P - Lecture 8 iPhone Application Development Scroll Views & Table Views Saturday, January 30, 2010 1
  • 3. iPhone SDK 3.2 Saturday, January 30, 2010 3
  • 4. iPhone SDK 3.2 • Support for iPad Saturday, January 30, 2010 3
  • 5. iPhone SDK 3.2 • Support for iPad • Beta Release Saturday, January 30, 2010 3
  • 6. iPhone SDK 3.2 • Support for iPad • Beta Release Enrollment in iPhone Developer Standard or Enterprise required Saturday, January 30, 2010 3
  • 7. Announcements • Paparazzi 1 due next Wednesday (2/3) Saturday, January 30, 2010 4
  • 8. Today’s Topics • Scroll views • Table views ■ Displaying data ■ Controlling appearance & behavior • UITableViewController • Table view cells Saturday, January 30, 2010 5
  • 10. UIScrollView • For displaying more content than can fit on the screen Saturday, January 30, 2010 7
  • 11. UIScrollView • For displaying more content than can fit on the screen • Handles gestures for panning and zooming Saturday, January 30, 2010 7
  • 12. UIScrollView • For displaying more content than can fit on the screen • Handles gestures for panning and zooming • Noteworthy subclasses: UITableView and UITextView Saturday, January 30, 2010 7
  • 20. Content Size contentSize.width contentSize.height Saturday, January 30, 2010 9
  • 21. Content Inset contentSize.width contentInset.top contentSize.height contentInset.bottom Saturday, January 30, 2010 10
  • 25. Content Inset contentInset.top Saturday, January 30, 2010 12
  • 26. Content Inset contentInset.top Saturday, January 30, 2010 13
  • 27. Content Inset contentInset.top contentInset.bottom Saturday, January 30, 2010 13
  • 28. Scroll Indicator Insets Saturday, January 30, 2010 14
  • 29. Scroll Indicator Insets Saturday, January 30, 2010 14
  • 30. Scroll Indicator Insets Saturday, January 30, 2010 14
  • 31. Scroll Indicator Insets scrollIndicatorInsets.top Saturday, January 30, 2010 14
  • 32. Content Offset Saturday, January 30, 2010 15
  • 33. Content Offset Saturday, January 30, 2010 15
  • 34. Content Offset Saturday, January 30, 2010 15
  • 35. contentSize.width contentInset.top contentSize.height contentInset.bottom contentInset.left contentInset.right Saturday, January 30, 2010 16
  • 37. contentOffset.x contentOffset.y Saturday, January 30, 2010 17
  • 40. contentOffset.x (-contentInset.left) contentOffset.y (-contentInset.top) Saturday, January 30, 2010 18
  • 41. Using a Scroll View Saturday, January 30, 2010 19
  • 42. Using a Scroll View • Create with the desired frame CGRect frame = CGRectMake(0, 0, 200, 200); scrollView = [[UIScrollView alloc] initWithFrame:frame]; Saturday, January 30, 2010 19
  • 43. Using a Scroll View • Create with the desired frame CGRect frame = CGRectMake(0, 0, 200, 200); scrollView = [[UIScrollView alloc] initWithFrame:frame]; • Add subviews (frames may extend beyond scroll view bounds) frame = CGRectMake(0, 0, 500, 500); myImageView = [[UIImageView alloc] initWithFrame:frame]; [scrollView addSubview:myImageView]; Saturday, January 30, 2010 19
  • 44. Using a Scroll View • Create with the desired frame CGRect frame = CGRectMake(0, 0, 200, 200); scrollView = [[UIScrollView alloc] initWithFrame:frame]; • Add subviews (frames may extend beyond scroll view bounds) frame = CGRectMake(0, 0, 500, 500); myImageView = [[UIImageView alloc] initWithFrame:frame]; [scrollView addSubview:myImageView]; • Set the content size scrollView.contentSize = CGSizeMake(500, 500); Saturday, January 30, 2010 19
  • 45. Extending Scroll View Behavior • Applications often want to know about scroll events Saturday, January 30, 2010 20
  • 46. Extending Scroll View Behavior • Applications often want to know about scroll events ■ When the scroll offset is changed Saturday, January 30, 2010 20
  • 47. Extending Scroll View Behavior • Applications often want to know about scroll events ■ When the scroll offset is changed ■ When dragging begins & ends Saturday, January 30, 2010 20
  • 48. 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 Saturday, January 30, 2010 20
  • 49. Extending with a Subclass • Create a subclass • Override methods to customize behavior Saturday, January 30, 2010 21
  • 50. Extending with a Subclass • Create a subclass • Override methods to customize behavior • Issues with this approach Saturday, January 30, 2010 21
  • 51. 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 Saturday, January 30, 2010 21
  • 52. 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 Saturday, January 30, 2010 21
  • 53. 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 Saturday, January 30, 2010 21
  • 54. Extending with Delegation • Delegate is a separate object • Clearly defined points of responsibility ■ Change behavior ■ Customize appearance • Loosely coupled with the object being extended Saturday, January 30, 2010 22
  • 56. UIScrollView Delegate @protocol UIScrollViewDelegate<NSObject> Saturday, January 30, 2010 23
  • 57. UIScrollView Delegate @protocol UIScrollViewDelegate<NSObject> @optional Saturday, January 30, 2010 23
  • 58. UIScrollView Delegate @protocol UIScrollViewDelegate<NSObject> @optional // Respond to interesting events - (void)scrollViewDidScroll:(UIScrollView *)scrollView; Saturday, January 30, 2010 23
  • 59. UIScrollView Delegate @protocol UIScrollViewDelegate<NSObject> @optional // Respond to interesting events - (void)scrollViewDidScroll:(UIScrollView *)scrollView; ... // Influence behavior - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; @end Saturday, January 30, 2010 23
  • 60. Implementing a Delegate Saturday, January 30, 2010 24
  • 61. Implementing a Delegate • Conform to the delegate protocol @interface MyController : NSObject <UIScrollViewDelegate> Saturday, January 30, 2010 24
  • 62. Implementing a Delegate • Conform to the delegate protocol @interface MyController : NSObject <UIScrollViewDelegate> • Implement all required methods and any optional methods - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // Do something in response to the new scroll position if (scrollView.contentOffset ...) { } } Saturday, January 30, 2010 24
  • 63. Zooming with a Scroll View Saturday, January 30, 2010 25
  • 64. Zooming with a Scroll View • Set the minimum, maximum, initial zoom scales scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = scrollView.size.width / myImage.size.width; Saturday, January 30, 2010 25
  • 65. Zooming with a Scroll View • Set the minimum, maximum, initial zoom scales scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = scrollView.size.width / myImage.size.width; • Implement delegate method for zooming - (UIView *)viewForZoomingInScrollView:(UIView *)view { return someViewThatWillBeScaled; } Saturday, January 30, 2010 25
  • 66. Set Zoom Scale - (void)setZoomScale:(float)scale animated:(BOOL); Saturday, January 30, 2010 26
  • 67. Set Zoom Scale - (void)setZoomScale:(float)scale animated:(BOOL); Saturday, January 30, 2010 26
  • 68. Set Zoom Scale - (void)setZoomScale:(float)scale animated:(BOOL); Saturday, January 30, 2010 26
  • 69. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 27
  • 70. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 27
  • 71. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 27
  • 72. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 28
  • 73. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 28
  • 74. Zoom to Rect - (void)zoomToRect:(CGRect)rect animated:(BOOL); Saturday, January 30, 2010 28
  • 77. Table Views • Display lists of content ■ Single column, multiple rows ■ Vertical scrolling ■ Large data sets • Powerful and ubiquitous in iPhone applications Saturday, January 30, 2010 31
  • 78. Table View Styles Saturday, January 30, 2010 32
  • 79. Table View Styles UITableViewStylePlain Saturday, January 30, 2010 32
  • 80. Table View Styles UITableViewStylePlain UITableViewStyleGrouped Saturday, January 30, 2010 32
  • 81. Table View Anatomy Plain Style Saturday, January 30, 2010 33
  • 82. Table View Anatomy Plain Style Table Header Saturday, January 30, 2010 33
  • 83. Table View Anatomy Plain Style Table Header Table Footer Saturday, January 30, 2010 33
  • 84. Table View Anatomy Plain Style Table Header Section Table Footer Saturday, January 30, 2010 33
  • 85. Table View Anatomy Plain Style Table Header Section Header Section Table Footer Saturday, January 30, 2010 33
  • 86. Table View Anatomy Plain Style Table Header Section Header Section Footer Section Table Footer Saturday, January 30, 2010 33
  • 87. Table View Anatomy Plain Style Table Header Section Header Table Cell Section Footer Section Table Footer Saturday, January 30, 2010 33
  • 88. Table View Anatomy Plain Style Table Header Section Header Table Cell Section Footer Section Table Footer Saturday, January 30, 2010 34
  • 89. Table View Anatomy Grouped Style Table Header Section Header Table Cell Section Footer Section Table Footer Saturday, January 30, 2010 35
  • 90. Using Table Views • Displaying your data in the table view • Customizing appearance & behavior Saturday, January 30, 2010 36
  • 91. Displaying Data in a Table View Saturday, January 30, 2010 37
  • 92. A Naïve Solution Saturday, January 30, 2010 38
  • 93. A Naïve Solution • Table views display a list of data, so use an array [myTableView setList:myListOfStuff]; Saturday, January 30, 2010 38
  • 94. A Naïve Solution • Table views display a list of data, so use an array [myTableView setList:myListOfStuff]; • Issues with this approach Saturday, January 30, 2010 38
  • 95. 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 Saturday, January 30, 2010 38
  • 96. 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 Saturday, January 30, 2010 38
  • 97. 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 Saturday, January 30, 2010 39
  • 99. 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; Saturday, January 30, 2010 40
  • 100. 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; • Provide cells for table view as needed // Required method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; Saturday, January 30, 2010 40
  • 101. Datasource Message Flow Datasource Saturday, January 30, 2010 41
  • 102. Datasource Message Flow numberOfSectionsInTableView: How many sections? Datasource Saturday, January 30, 2010 41
  • 103. Datasource Message Flow numberOfSectionsInTableView: Datasource 5 Saturday, January 30, 2010 41
  • 104. Datasource Message Flow Datasource Saturday, January 30, 2010 42
  • 105. Datasource Message Flow tableView:numberOfRowsInSection: How many rows in section 0? Datasource Saturday, January 30, 2010 42
  • 106. Datasource Message Flow tableView:numberOfRowsInSection: Datasource 1 Saturday, January 30, 2010 42
  • 107. Datasource Message Flow Datasource Saturday, January 30, 2010 43
  • 108. Datasource Message Flow tableView:cellForRowAtIndexPath: What to display at section 0, row 0? Datasource Saturday, January 30, 2010 43
  • 109. Datasource Message Flow tableView:cellForRowAtIndexPath: Datasource Cell with text “John Appleseed” Saturday, January 30, 2010 43
  • 110. NSIndexPath • Generic class in Foundation • Path to a specific node in a tree of nested arrays 0 1 2 3 4 Saturday, January 30, 2010 44
  • 111. NSIndexPath • Generic class in Foundation • Path to a specific node in a tree of nested arrays 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Saturday, January 30, 2010 44
  • 112. NSIndexPath and Table Views • Cell location described with an index path ■ Section index + row index Saturday, January 30, 2010 45
  • 113. 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 Saturday, January 30, 2010 45
  • 114. Single Section Table View Saturday, January 30, 2010 46
  • 115. Single Section Table View • Return the number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myStrings count]; } Saturday, January 30, 2010 46
  • 116. Single Section Table View • Return the number of rows - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myStrings count]; } • Provide a cell when requested - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = ...; cell.textLabel.text = [myStrings objectAtIndex:indexPath.row] return [cell autorelease]; } Saturday, January 30, 2010 46
  • 118. Cell Reuse • When asked for a cell, it would be expensive to create a new cell each time. Saturday, January 30, 2010 47
  • 119. Cell Reuse • When asked for a cell, it would be expensive to create a new cell each time. - (UITableViewCell *)dequeueReusableCellWithIdentifier: (NSString *)identifier; Saturday, January 30, 2010 47
  • 120. 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; } Saturday, January 30, 2010 47
  • 121. Triggering Updates • When is the datasource asked for its data? Saturday, January 30, 2010 48
  • 122. Triggering Updates • When is the datasource asked for its data? ■ When a row becomes visible Saturday, January 30, 2010 48
  • 123. 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]; } Saturday, January 30, 2010 48
  • 124. Section and Row Reloading Saturday, January 30, 2010 49
  • 125. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 126. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 127. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 128. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 129. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 130. Section and Row Reloading - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; Saturday, January 30, 2010 49
  • 131. Additional Datasource Methods • Titles for section headers and footers • Allow editing and reordering cells Saturday, January 30, 2010 50
  • 132. Appearance & Behavior Saturday, January 30, 2010 51
  • 133. UITableView Delegate • Customize appearance and behavior • Keep application logic separate from view • Often the same object as datasource Saturday, January 30, 2010 52
  • 134. Table View Appearance & Behavior Saturday, January 30, 2010 53
  • 135. Table View Appearance & Behavior • Customize appearance of table view cell - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; Saturday, January 30, 2010 53
  • 136. Table View Appearance & Behavior • Customize appearance of table view cell - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; • Validate and respond to selection changes - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; Saturday, January 30, 2010 53
  • 137. Row Selection in Table Views • In iPhone applications, rows rarely stay selected • Selecting a row usually triggers an event Saturday, January 30, 2010 54
  • 138. Row Selection in Table Views • In iPhone applications, rows rarely stay selected • Selecting a row usually triggers an event Saturday, January 30, 2010 54
  • 141. Responding to Selection // For a navigation hierarchy... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Saturday, January 30, 2010 56
  • 142. Responding to Selection // For a navigation hierarchy... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Get the row and the object it represents Saturday, January 30, 2010 56
  • 143. 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 Saturday, January 30, 2010 56
  • 144. 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]; Saturday, January 30, 2010 56
  • 145. 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 Saturday, January 30, 2010 56
  • 146. 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 = ...; Saturday, January 30, 2010 56
  • 147. 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; Saturday, January 30, 2010 56
  • 148. 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]; } Saturday, January 30, 2010 56
  • 149. 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; } } Saturday, January 30, 2010 57
  • 152. UITableViewController • Convenient starting point for view controller with a table view ■ Table view is automatically created ■ Controller is table view’s delegate and datasource Saturday, January 30, 2010 59
  • 153. 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 Saturday, January 30, 2010 59
  • 154. Table View Cells Saturday, January 30, 2010 60
  • 156. Designated Initializer - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier; Saturday, January 30, 2010 61
  • 157. Designated Initializer ECATED EPR - (id)initWithFrame:(CGRect)frame DreuseIdentifier:(NSString *)reuseIdentifier; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; Saturday, January 30, 2010 61
  • 159. Cell Styles UITableViewCellStyleDefault Saturday, January 30, 2010 62
  • 160. Cell Styles UITableViewCellStyleDefault UITableViewCellStyleSubtitle Saturday, January 30, 2010 62
  • 161. Cell Styles UITableViewCellStyleDefault UITableViewCellStyleSubtitle UITableViewCellStyleValue1 Saturday, January 30, 2010 62
  • 162. Cell Styles UITableViewCellStyleDefault UITableViewCellStyleSubtitle UITableViewCellStyleValue1 UITableViewCellStyleValue2 Saturday, January 30, 2010 62
  • 163. Basic properties • UITableViewCell has an image view and one or two text labels Saturday, January 30, 2010 63
  • 164. 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”; Saturday, January 30, 2010 63
  • 165. Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; Saturday, January 30, 2010 64
  • 166. Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; UITableViewCellAccessoryDisclosureIndicator Saturday, January 30, 2010 64
  • 167. Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailDisclosureButton Saturday, January 30, 2010 64
  • 168. Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailDisclosureButton - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { // Only for the blue disclosure button NSUInteger row = indexPath.row; ... } Saturday, January 30, 2010 64
  • 169. Accessory Types // UITableView delegate method - (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath; UITableViewCellAccessoryDisclosureIndicator UITableViewCellAccessoryDetailDisclosureButton UITableViewCellAccessoryCheckmark - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { // Only for the blue disclosure button NSUInteger row = indexPath.row; ... } Saturday, January 30, 2010 64
  • 170. 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 Saturday, January 30, 2010 65
  • 171. 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]; } Saturday, January 30, 2010 65