iOS - Vue tableau

Utilisation de la vue tableau

Il est utilisé pour afficher une vue déroulante verticalement qui se compose d'un certain nombre de cellules (généralement des cellules réutilisables). Il a des fonctionnalités spéciales comme les en-têtes, les pieds de page, les lignes et la section.

Propriétés importantes

  • delegate
  • dataSource
  • rowHeight
  • sectionFooterHeight
  • sectionHeaderHeight
  • separatorColor
  • tableHeaderView
  • tableFooterView

Méthodes importantes

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath
- (void)reloadData
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
- (NSArray *)visibleCells

Exemple de code et étapes

Step 1 - Ajoutons une tableview dans ViewController.xib comme indiqué ci-dessous.

Step 2 - Ensemble delegate et dataSource à file ownerpour la vue de table en cliquant avec le bouton droit de la souris et en sélectionnant la source de données et le délégué. La configuration de dataSource est indiquée ci-dessous.

Step 3 - Créer un IBOutlet for tableView et nommez-le comme myTableView. Il est montré dans les images suivantes.

Step 4 - Ajoutez ensuite un NSMutableArray pour contenir les données à afficher dans la vue de table.

Step 5 - Notre ViewController doit adopter le UITableViewDataSource et UITableViewDelegateprotocoles. leViewController.h devrait ressembler à l'illustration ci-dessous.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,
   UITableViewDelegate> {
   IBOutlet UITableView *myTableView;
   NSMutableArray *myData;
}
@end

Step 6- Nous devons implémenter le délégué tableview requis et les méthodes dataSource. La mise à jourViewController.m est comme suit -

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   // table view data is being set here	
   myData = [[NSMutableArray alloc]initWithObjects:
   @"Data 1 in array",@"Data 2 in array",@"Data 3 in array",
   @"Data 4 in array",@"Data 5 in array",@"Data 5 in array",
   @"Data 6 in array",@"Data 7 in array",@"Data 8 in array",
   @"Data 9 in array", nil];
   // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

#pragma mark - Table View Data source 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
   (NSInteger)section {
   return [myData count]/2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
   (NSIndexPath *)indexPath {
   static NSString *cellIdentifier = @"cellID";
    
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
   cellIdentifier];
   
   if (cell == nil) {
      cell = [[UITableViewCell alloc]initWithStyle:
      UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
   }
   
   NSString *stringForCell;
   
   if (indexPath.section == 0) {
      stringForCell= [myData objectAtIndex:indexPath.row];
   } else if (indexPath.section == 1) {
      stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]/2];
   }
   [cell.textLabel setText:stringForCell];
   return cell;
}

// Default is 1 if not implemented
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:
   (NSInteger)section {
   NSString *headerTitle;
   
   if (section==0) {
      headerTitle = @"Section 1 Header";
   } else {
      headerTitle = @"Section 2 Header";
   }
   return headerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:
   (NSInteger)section {
   NSString *footerTitle;
      
   if (section==0) {
      footerTitle = @"Section 1 Footer";
   } else {
      footerTitle = @"Section 2 Footer";
   }
   return footerTitle;
}

#pragma mark - TableView delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
   (NSIndexPath *)indexPath {
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   NSLog(@"Section:%d Row:%d selected and its data is %@",
   indexPath.section,indexPath.row,cell.textLabel.text);
}
@end

Step 7 - Lorsque nous exécutons l'application, nous obtenons ce qui suit output -