SlideShare una empresa de Scribd logo
1 de 33
Descargar para leer sin conexión
squeek school

                              Разработка приложений для iOS
                                        Занятие #5




понедельник, 16 мая 2011 г.
Табличное представление +
                                скроллинг



             • UITableView
             • UIScrollView
             • делегаты

понедельник, 16 мая 2011 г.
Table View Styles              TableView
                              UITableViewStylePlain   UITableViewStyleGrouped




понедельник, 16 мая 2011 г.
анатомия TableView
                              Table View Anatomy
                                             Plain Style

                              Table Header
                                                           Section Header


                                                           Table Cell
                                                           Section Footer


                                   Section


                              Table Footer




понедельник, 16 мая 2011 г.
анатомия TableView
                             Table View Anatomy
                                        Grouped Style

                      Table Header
                                                        Section Header


                                                        Table Cell
                                                        Section Footer



                              Section



                       Table Footer




понедельник, 16 мая 2011 г.
использование
                                  TableView

                    • использование делегатов,
                              предоставляющих данные
                    • просит то, что сейчас нужно


понедельник, 16 мая 2011 г.
UITableViewDataSource

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

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

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




понедельник, 16 мая 2011 г.
Datasource Message Flow
                                     numberOfSectionsInTableView:




                                                  How many
                                                   sections?

                                                        Datasource




Saturday, January 30, 2010                                           41

понедельник, 16 мая 2011 г.
Datasource Message Flow
                                    tableView:numberOfRowsInSection:




                                                   How many rows
                                                    in section 0?

                                                        Datasource




Saturday, January 30, 2010                                           42


понедельник, 16 мая 2011 г.
Datasource Message Flow
                                     tableView:cellForRowAtIndexPath:




                                                 What to display at
                                                 section 0, row 0?

                                                           Datasource




Saturday, January 30, 2010                                              43

 понедельник, 16 мая 2011 г.
NSIndexPath
                    •         массив int

                    •         секция + строка
                    •         [NSIndexPath indexPathForRow:(NSUInteger)row inSection:
                              (NSUInteger)section];


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




понедельник, 16 мая 2011 г.
reuse - чтобы быстрее
   - (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;

   }

                              identifier - любой


понедельник, 16 мая 2011 г.
Cell Styles
                      initWithStyle             -   UITableViewCellStyle



                 UITableViewCellStyleDefault

                 UITableViewCellStyleSubtitle




                 UITableViewCellStyleValue1

                 UITableViewCellStyleValue2




понедельник, 16 мая 2011 г.
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”;




понедельник, 16 мая 2011 г.
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;
         ...
       }
понедельник, 16 мая 2011 г.
перезагрузить
                      - (void)viewWillAppear:(BOOL)animated
                      {
                        [super viewWillAppear:animated];
                          [self.tableView reloadData];
                      }




понедельник, 16 мая 2011 г.
добавление по ходу
         - (void)insertSections:(NSIndexSet *)sections withRowAnimation:
         (UITableViewRowAnimation)animation;

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


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




понедельник, 16 мая 2011 г.
UITableViewDelegate


                    • поведение + внешний вид
                    • логика отдельно от данных!


понедельник, 16 мая 2011 г.
выбор строки


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




понедельник, 16 мая 2011 г.
обычно...
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
  *)indexPath {

  NSUInteger row = indexPath.row
  id objectToDisplay = [myObjects objectAtIndex:row];


  MyViewController *myViewController = ...;
  myViewController.object = objectToDisplay;
  [self.navigationController pushViewController:myViewController animated:YES];

  }




понедельник, 16 мая 2011 г.
UITableViewController
                      = UITableView + делегаты




понедельник, 16 мая 2011 г.
UIScrollView
                        Scrolling Examples




понедельник, 16 мая 2011 г.
Content Size
                       контент
                                              contentSize.width




                         contentSize.height




понедельник, 16 мая 2011 г.
Content Inset

                                 contentInset.top




                              contentInset.bottom




понедельник, 16 мая 2011 г.
Content Inset                       contentSize.width

                                                                       contentInset.top




                              contentSize.height




                                                                       contentInset.bottom



понедельник, 16 мая 2011 г.
начало скролла
                                Scroll Indicator Insets

                              scrollIndicatorInsets.top




понедельник, 16 мая 2011 г.
Content
                              Offset




понедельник, 16 мая 2011 г.
contentOffset.x

                        contentOffset.y




понедельник, 16 мая 2011 г.
contentSize.width
                                                                   contentInset.top




           contentSize.height




                                                                   contentInset.bottom
                                contentInset.left       contentInset.right



Saturday, January 30, 2010                                                               16
  понедельник, 16 мая 2011 г.
как использовать?
                   CGRect frame = CGRectMake(0, 0, 200, 200);
                    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];

                     frame = CGRectMake(0, 0, 500, 500);
                     UIImage *image = [UIImage imageNamed:@"tree.jpg"];
                     UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];
                     myImageView.frame = frame;
                     [scrollView addSubview:myImageView];

                    scrollView.contentSize = CGSizeMake(500, 500);
                    [self.view addSubview:scrollView];




понедельник, 16 мая 2011 г.
Делегат
                              UIScrollViewDelegate

            @protocol UIScrollViewDelegate<NSObject>
            @optional

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

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




понедельник, 16 мая 2011 г.
Масштабирование
        scrollView.maximumZoomScale = 2.0;
        scrollView.minimumZoomScale = scrollView.frame.size.width /
        myImage.frame.size.width;



        и реализовать метод делегата:

        - (UIView *)viewForZoomingInScrollView:(UIScrollView *)view {
          return someViewThatWillBeScaled;
        }




понедельник, 16 мая 2011 г.
Zoom to Rect




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

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

urday, January 30, 2010                                                          26


day, January мая 2011 г.
 понедельник, 16
                 30, 2010

Más contenido relacionado

Más de Sergey Pronin

App in the Air Internship 2018
App in the Air Internship 2018App in the Air Internship 2018
App in the Air Internship 2018Sergey Pronin
 
Things you might have missed from CoreData
Things you might have missed from CoreDataThings you might have missed from CoreData
Things you might have missed from CoreDataSergey Pronin
 
Mera Dev Fest - Swift vs. Obj-C
Mera Dev Fest - Swift vs. Obj-CMera Dev Fest - Swift vs. Obj-C
Mera Dev Fest - Swift vs. Obj-CSergey Pronin
 
Empatika Design Hours
Empatika Design HoursEmpatika Design Hours
Empatika Design HoursSergey Pronin
 
Greenfield Feedback Squeek
Greenfield Feedback SqueekGreenfield Feedback Squeek
Greenfield Feedback SqueekSergey Pronin
 

Más de Sergey Pronin (8)

App in the Air Internship 2018
App in the Air Internship 2018App in the Air Internship 2018
App in the Air Internship 2018
 
Things you might have missed from CoreData
Things you might have missed from CoreDataThings you might have missed from CoreData
Things you might have missed from CoreData
 
Mera Dev Fest - Swift vs. Obj-C
Mera Dev Fest - Swift vs. Obj-CMera Dev Fest - Swift vs. Obj-C
Mera Dev Fest - Swift vs. Obj-C
 
Swift School #3
Swift School #3Swift School #3
Swift School #3
 
Swift School #2
Swift School #2Swift School #2
Swift School #2
 
Empatika Design Hours
Empatika Design HoursEmpatika Design Hours
Empatika Design Hours
 
Greenfield Feedback Squeek
Greenfield Feedback SqueekGreenfield Feedback Squeek
Greenfield Feedback Squeek
 
Squeek School #3
Squeek School #3Squeek School #3
Squeek School #3
 

Squeek School #5

  • 1. squeek school Разработка приложений для iOS Занятие #5 понедельник, 16 мая 2011 г.
  • 2. Табличное представление + скроллинг • UITableView • UIScrollView • делегаты понедельник, 16 мая 2011 г.
  • 3. Table View Styles TableView UITableViewStylePlain UITableViewStyleGrouped понедельник, 16 мая 2011 г.
  • 4. анатомия TableView Table View Anatomy Plain Style Table Header Section Header Table Cell Section Footer Section Table Footer понедельник, 16 мая 2011 г.
  • 5. анатомия TableView Table View Anatomy Grouped Style Table Header Section Header Table Cell Section Footer Section Table Footer понедельник, 16 мая 2011 г.
  • 6. использование TableView • использование делегатов, предоставляющих данные • просит то, что сейчас нужно понедельник, 16 мая 2011 г.
  • 7. UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)table; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; понедельник, 16 мая 2011 г.
  • 8. Datasource Message Flow numberOfSectionsInTableView: How many sections? Datasource Saturday, January 30, 2010 41 понедельник, 16 мая 2011 г.
  • 9. Datasource Message Flow tableView:numberOfRowsInSection: How many rows in section 0? Datasource Saturday, January 30, 2010 42 понедельник, 16 мая 2011 г.
  • 10. Datasource Message Flow tableView:cellForRowAtIndexPath: What to display at section 0, row 0? Datasource Saturday, January 30, 2010 43 понедельник, 16 мая 2011 г.
  • 11. NSIndexPath • массив int • секция + строка • [NSIndexPath indexPathForRow:(NSUInteger)row inSection: (NSUInteger)section]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { UITableViewCell *cell = ...; cell.textLabel.text = [myStrings objectAtIndex:indexPath.row] return [cell autorelease]; } понедельник, 16 мая 2011 г.
  • 12. reuse - чтобы быстрее - (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; } identifier - любой понедельник, 16 мая 2011 г.
  • 13. Cell Styles initWithStyle - UITableViewCellStyle UITableViewCellStyleDefault UITableViewCellStyleSubtitle UITableViewCellStyleValue1 UITableViewCellStyleValue2 понедельник, 16 мая 2011 г.
  • 14. 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”; понедельник, 16 мая 2011 г.
  • 15. 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; ... } понедельник, 16 мая 2011 г.
  • 16. перезагрузить - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView reloadData]; } понедельник, 16 мая 2011 г.
  • 17. добавление по ходу - (void)insertSections:(NSIndexSet *)sections withRowAnimation: (UITableViewRowAnimation)animation; - (void)deleteSections:(NSIndexSet *)sections withRowAnimation: (UITableViewRowAnimation)animation; - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; понедельник, 16 мая 2011 г.
  • 18. UITableViewDelegate • поведение + внешний вид • логика отдельно от данных! понедельник, 16 мая 2011 г.
  • 19. выбор строки - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath; понедельник, 16 мая 2011 г.
  • 20. обычно... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = indexPath.row id objectToDisplay = [myObjects objectAtIndex:row]; MyViewController *myViewController = ...; myViewController.object = objectToDisplay; [self.navigationController pushViewController:myViewController animated:YES]; } понедельник, 16 мая 2011 г.
  • 21. UITableViewController = UITableView + делегаты понедельник, 16 мая 2011 г.
  • 22. UIScrollView Scrolling Examples понедельник, 16 мая 2011 г.
  • 23. Content Size контент contentSize.width contentSize.height понедельник, 16 мая 2011 г.
  • 24. Content Inset contentInset.top contentInset.bottom понедельник, 16 мая 2011 г.
  • 25. Content Inset contentSize.width contentInset.top contentSize.height contentInset.bottom понедельник, 16 мая 2011 г.
  • 26. начало скролла Scroll Indicator Insets scrollIndicatorInsets.top понедельник, 16 мая 2011 г.
  • 27. Content Offset понедельник, 16 мая 2011 г.
  • 28. contentOffset.x contentOffset.y понедельник, 16 мая 2011 г.
  • 29. contentSize.width contentInset.top contentSize.height contentInset.bottom contentInset.left contentInset.right Saturday, January 30, 2010 16 понедельник, 16 мая 2011 г.
  • 30. как использовать? CGRect frame = CGRectMake(0, 0, 200, 200); UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame]; frame = CGRectMake(0, 0, 500, 500); UIImage *image = [UIImage imageNamed:@"tree.jpg"]; UIImageView *myImageView = [[UIImageView alloc] initWithImage:image]; myImageView.frame = frame; [scrollView addSubview:myImageView]; scrollView.contentSize = CGSizeMake(500, 500); [self.view addSubview:scrollView]; понедельник, 16 мая 2011 г.
  • 31. Делегат UIScrollViewDelegate @protocol UIScrollViewDelegate<NSObject> @optional - (void)scrollViewDidScroll:(UIScrollView *)scrollView; ... - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; @end понедельник, 16 мая 2011 г.
  • 32. Масштабирование scrollView.maximumZoomScale = 2.0; scrollView.minimumZoomScale = scrollView.frame.size.width / myImage.frame.size.width; и реализовать метод делегата: - (UIView *)viewForZoomingInScrollView:(UIScrollView *)view { return someViewThatWillBeScaled; } понедельник, 16 мая 2011 г.
  • 33. Zoom to Rect - (void)setZoomScale:(float)scale animated:(BOOL); - (void)zoomToRect:(CGRect)rect animated:(BOOL); urday, January 30, 2010 26 day, January мая 2011 г. понедельник, 16 30, 2010