90
Naviga&on and Tab Bar Controllers and Table View

08_NavigationTabBarController

Embed Size (px)

DESCRIPTION

08_NavigationTabBarController

Citation preview

Page 1: 08_NavigationTabBarController

Naviga&on  and  Tab  Bar  Controllers  and  Table  View  

Page 2: 08_NavigationTabBarController

UINaviga)onController  

•  Stack  of  view  controllers  •  Naviga)on  bar  

Page 3: 08_NavigationTabBarController

How  It  Fits  Together  

•  Top  view  controller’s  view  •  Top  view  controller’s  )tle  •  Previous  view  controller’s  )tle  •  Top  view  controller’s  toolbar  items  (iPhone  OS  3.0)  

Page 4: 08_NavigationTabBarController

The  views  of  a  naviga)on  interface  

Page 5: 08_NavigationTabBarController

Objects  managed  by    the  naviga)on  controller  

Page 6: 08_NavigationTabBarController

 The  naviga)on  stack  

Page 7: 08_NavigationTabBarController

Loading  Your  Naviga&on  Interface  from  a  Nib  File  

 

Page 8: 08_NavigationTabBarController

Crea)ng  a  Naviga)on  Controller  Programma)cally  

-­‐  (void)applica)onDidFinishLaunching:(UIApplica)on  *)applica)on{    UIViewController  *rootController  =  [[MyRootViewController  alloc]  init];    naviga)onController  =  [[UINaviga)onController  alloc]  initWithRootViewController:rootController];    [rootController  release];    window  =  [[UIWindow  alloc]  initWithFrame:[[UIScreen  mainScreen]  bounds]];    [window  addSubview:naviga)onController.view];    [window  makeKeyAndVisible];}  

Page 9: 08_NavigationTabBarController

Modifying  the  Naviga)on  Stack  •  Push  to  add  a  view  controller  -­‐  (void)pushViewController:(UIViewController  *)viewController            animated:(BOOL)animated;  

•  Pop  to  remove  a  view  controller  -­‐  (UIViewController  *)popViewControllerAnimated:(BOOL)animated;  

•  Set  to  change  the  en&re  stack  of  view  controllers  •  (iPhone  OS  3.0)  -­‐  (void)setViewControllers:(NSArray  *)viewControllers          animated:(BOOL)animated;    

Page 10: 08_NavigationTabBarController

Pushing  Your  First  View  Controller  -­‐  (void)applica)onDidFinishLaunching  {  //  Create  a  naviga)on  controller  navController  =  [[UINaviga)onController  alloc]  init];    //  Push  the  first  view  controller  on  the  stack  [navController  pushViewController:firstViewController                              animated:NO];  

 //  Add  the  naviga)on  controller’s  view  to  the  window  [window  addSubview:navController.view];    }    

Page 11: 08_NavigationTabBarController

In  Response  to  User  Ac)ons  •  Push  from  within  a  view  controller  on  the  stack  -­‐  (void)someAc)on:(id)sender  {    //  Poten)ally  create  another  view  controller    UIViewController  *viewController  =  ...;  

   [self.naviga)onController  pushViewController:viewController                          animated:YES];  

}  •  Almost  never  call  pop  directly!  

–  Automa)cally  invoked  by  the  back  bu_on  

Page 12: 08_NavigationTabBarController

Messages  sent  during  stack  changes  

Page 13: 08_NavigationTabBarController
Page 14: 08_NavigationTabBarController

A  Controller  for  Each  Screen  

Page 15: 08_NavigationTabBarController

Connec)ng  View  Controllers  

•  Mul)ple  view  controllers  may  need  to  share  data  

•  One  may  need  to  know  about  what  another  is  doing  – Watch  for  added,  removed  or  edited  data  – Other  interes)ng  events  

Page 16: 08_NavigationTabBarController

How  Not  To  Share  Data  •  Global  variables  or  singletons  

–  This  includes  your  applica&on  delegate!  •  Direct  dependencies  make  your  code  less  reusable  

–  And  more  difficult  to  debug  &  test  

Page 17: 08_NavigationTabBarController

Best  Prac)ces  for  Data  Flow  •  Figure  out  exactly  what  needs  to  be  communicated  •  Define  input  parameters  for  your  view  controller  •  For  communica)ng  back  up  the  hierarchy,  use  loose  coupling  –  Define  a  generic  interface  for  observers  (like  delega)on)  

Page 18: 08_NavigationTabBarController

Customizing  Naviga)on  

•  Bu_ons  or  custom  controls  •  Interact  with  the  en)re  screen  

Page 19: 08_NavigationTabBarController

Naviga)on  bar  styles  

Page 20: 08_NavigationTabBarController

The  objects  associated  with  a  naviga)on  bar  

Page 21: 08_NavigationTabBarController

UINaviga)onItem  

•  Describes  appearance  of  the  naviga)on  bar  – Title  string  or  custom  )tle  view  – Lee  &  right  bar  bu_ons  – More  proper)es  defined  in  UINaviga)onBar.h  

•  Every  view  controller  has  a  naviga&on  item  for  customizing  – Displayed  when  view  controller  is  on  top  of  the  stack  

Page 22: 08_NavigationTabBarController

Naviga)on  Item  Ownership  

Page 23: 08_NavigationTabBarController

The  objects  associated  with  a  naviga)on  bar  

Page 24: 08_NavigationTabBarController

Displaying  a  Title  

•  UIViewController  already  has  a  )tle  property  – @property(nonatomic,copy)  NSString  *)tle;  

•  Naviga)on  item  inherits  automa)cally  – Previous  view  controller’s  )tle  is  displayed  in  back  bu_on  

     viewController.)tle  =  @“Detail”;  

Page 25: 08_NavigationTabBarController

Lee  &  Right  Bu_ons  

•  UIBarBu_onItem  – Special  object,  defines  appearance  &  behavior  for  items  in  naviga)on  bars  and  toolbars  

•  Display  a  string,  image  or  predefined  system  item  

•  Target  +  ac)on  (like  a  regular  bu_on)  

Page 26: 08_NavigationTabBarController

Text  Bar  Bu_on  Item  -­‐  (void)viewDidLoad  {    UIBarBu_onItem  *fooBu_on  =  [[UIBarBu_onItem  alloc]      initWithTitle:@"Foo”      style:UIBarBu_onItemStyleBordered      target:self      ac)on:@selector(foo:)];  

   self.naviga)onItem.leeBarBu_onItem  =  fooBu_on;  

   [fooBu_on  release];  

}  

Page 27: 08_NavigationTabBarController

System  Bar  Bu_on  Item  -­‐  (void)viewDidLoad  {    UIBarBu_onItem  *addBu_on  =  [[UIBarBu_onItem  alloc]      initWithBarBu_onSystemItem:UIBarBu_onSystemItemAdd      style:UIBarBu_onItemStyleBordered      target:self      ac)on:@selector(add:)];  

 self.naviga)onItem.rightBarBu_onItem  =  addBu_on;    [addBu_on  release];  }  

Page 28: 08_NavigationTabBarController

Edit/Done  Bu_on  •  Very  common  pa_ern  •  Every  view  controller  has  one  available  

–  Target/ac)on  already  set  up  

self.naviga)onItem.leeBarBu_onItem  =  self.editBu_onItem;    //  Called  when  the  user  toggles  the  edit/done  bu_on  -­‐  (void)setEdi)ng:(BOOL)edi)ng  animated:(BOOL)animated  {    //  Update  appearance  of  views  

}  

Page 29: 08_NavigationTabBarController

Custom  Title  View  •  Arbitrary  view  in  place  of  the  )tle  

UISegmentedControl  *segmentedControl  =  ...  self.naviga)onItem.)tleView  =  segmentedControl;  [segmentedControl  release];  

Page 30: 08_NavigationTabBarController

Back  Bu_on  •  Some)mes  a  shorter  back  bu_on  is  needed  

self.)tle  =  @“Hello  there,  CS193P!”;    UIBarBu_onItem  *heyBu_on  =  [[UIBarBu_onItem  alloc]              initWithTitle:@“Hey!”              ...];  

self.naviga)onItem.backBu_onItem  =  heyBu_on;    [heyBu_on  release];  

Page 31: 08_NavigationTabBarController

//  View  3  -­‐  Custom  right  bar  bu_on  with  a  view  UISegmentedControl  *segmentedControl  =  [[UISegmentedControl  alloc]  initWithItems:  [NSArray  arrayWithObjects:  [UIImage  imageNamed:@"up.png"],  [UIImage  imageNamed:@"down.png"],  nil]];    [segmentedControl  addTarget:self  ac)on:@selector(segmentAc)on:)  forControlEvents:UIControlEventValueChanged];  segmentedControl.frame  =  CGRectMake(0,  0,  90,  kCustomBu_onHeight);  segmentedControl.segmentedControlStyle  =  UISegmentedControlStyleBar;segmentedControl.momentary  =  YES;  defaultTintColor  =  [segmentedControl.)ntColor  retain];    //  keep  track  of  this  for  later    UIBarBu_onItem  *segmentBarItem  =  [[UIBarBu_onItem  alloc]  initWithCustomView:segmentedControl];  [segmentedControl  release];    self.naviga)onItem.rightBarBu_onItem  =  segmentBarItem;  [segmentBarItem  release];  

Page 32: 08_NavigationTabBarController

Toolbar  items  in  a  naviga)on  interface  

Page 33: 08_NavigationTabBarController

(void)configureToolbarItems  {          UIBarBu_onItem  *flexibleSpaceBu_onItem  =  [[UIBarBu_onItem  alloc]    initWithBarBu_onSystemItem:UIBarBu_onSystem  ItemFlexibleSpace  target:nil  ac)on:nil];          //  Create  and  configure  the  segmented  control        UISegmentedControl  *sortToggle  =  [[UISegmentedControl  alloc]  initWithItems:[NSArray  arrayWithObjects:@"Ascending”,                                                                      @"Descending",  nil]];        sortToggle.segmentedControlStyle  =  UISegmentedControlStyleBar;        sortToggle.selectedSegmentIndex  =  0;        [sortToggle  addTarget:self  ac)on:@selector(toggleSor)ng:)  forControlEvents:UIControlEventValueChanged];                  

// Create the bar button item for the segmented control UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc] initWithCustomView:sortToggle]; [sortToggle release]; // Set our toolbar items self.toolbarItems = [NSArray arrayWithObjects: flexibleSpaceButtonItem, sortToggleButtonItem, flexibleSpaceButtonItem, nil]; [sortToggleButtonItem release]; [flexibleSpaceButtonItem release]; }

Page 34: 08_NavigationTabBarController

UITabBarController  

•  Array  of  view  controllers  •  Tab  bar  

Page 35: 08_NavigationTabBarController

How  It  Fits  Together  

•  Selected  view  controller’s  view  •  All  view  controllers’  )tles  

Page 36: 08_NavigationTabBarController

The  views  of  a  tab  bar  interface  

Page 37: 08_NavigationTabBarController

A  tab  bar  controller  and  its  associated  view  controllers  

Page 38: 08_NavigationTabBarController

Tab  bar  items  of  the  iPod  applica)on  

Page 39: 08_NavigationTabBarController

Nib  file  containing  a  tab  bar  interface  

Page 40: 08_NavigationTabBarController

Seong  Up  a  Tab  Bar  Controller�-­‐  (void)applica)onDidFinishLaunching    //  Create  a  tab  bar  controller    tabBarController  =  [[UITabBarController  alloc]  init];  

   //  Set  the  array  of  view  controllers    tabBarController.viewControllers  =  myViewControllers;  

   //  Add  the  tab  bar  controller’s  view  to  the  window    [window  addSubview:tabBarController.view];

}  

Page 41: 08_NavigationTabBarController

Tab  Bar  Appearance

•  View  controllers  can  define  their  appearance  in  the  tab  bar  

•  UITabBarItem  – Title  +  image  or  system  item  

•  Each  view  controller  comes  with  a  tab  bar  item  for  customizing

Page 42: 08_NavigationTabBarController

Crea)ng  Tab  Bar  Items •  Title  and  image    

-­‐  (void)viewDidLoad  {  

 UITabBarItem  *item  =  [[UITabBarItem  alloc]          initWithTitle:@“Playlists”          image:[UIImage  imageNamed:@“music.png”]          tag:0];    self.tabBarItem  =  item;    [item  release];  

}

Page 43: 08_NavigationTabBarController

Crea)ng  Tab  Bar  Items •  System  item  

-­‐  (void)viewDidLoad  {    UITabBarItem  *item  =  [[UITabBarItem  alloc]                      initWithTabBarSystemItem:                      UITabBarSystemItemBookmarks                      tag:0]    self.tabBarItem  =  item;    [item  release];  

}

Page 44: 08_NavigationTabBarController

Crea)ng  the  view  controller’s  tab  bar  item  

-­‐  (id)init  {        if  (self  =  [super  initWithNibName:@"MyViewController"  bundle:nil])  {              self.)tle  =  @"My  View  Controller”;              UIImage*  anImage  =  [UIImage  imageNamed:@"MyViewControllerImage.png"];              UITabBarItem*  theItem  =  [[UITabBarItem  alloc]  initWithTitle:@"Home"  image:anImage  tag:0];              self.tabBarItem  =  theItem;              [theItem  release];        }        return  self;  }  

Page 45: 08_NavigationTabBarController

Removing  the  current  tab  -­‐  (IBAc)on)processUserInforma)on:(id)sender{    

//  Call  some  app-­‐specific  method  to  validate  the  user  data.    //  If  the  custom  method  returns  YES,  remove  the  tab.    if  ([self  userDataIsValid])  {    NSMutableArray*  newArray  =  [NSMutableArray  arrayWithArray:self.tabBarController.viewControllers];  [newArray  removeObject:self];    [self.tabBarController  setViewControllers:newArray  animated:YES];    }  

}    

Page 46: 08_NavigationTabBarController

   

Preven)ng  the  selec)on  of  tabs      

- (BOOL)tabBarController:(UITabBarController *)aTabBar shouldSelectViewController:(UIViewController *)viewController { if (![self hasValidLogin] && (viewController != [aTabBar.viewControllers objectAtIndex:0]) ) { // Disable all but the first tab. return NO; } return YES; }

Page 47: 08_NavigationTabBarController

More  View  Controllers  

Page 48: 08_NavigationTabBarController

Tab  Bar  +  Naviga)on  Controllers Mul)ple  parallel  hierarchies

Page 49: 08_NavigationTabBarController

Tab  Bar  +  Naviga)on  Controllers

Page 50: 08_NavigationTabBarController

Nes)ng  Naviga)on  Controllers •  Create  a  tab  bar  controller  

 tabBarController  =  [[UITabBarController  alloc]  init];  

•  Create  each  naviga)on  controller    navController  =  [[UINaviga)onController  alloc]  init];    [navController  pushViewController:firstViewController          animated:NO];  

•  Add  them  to  the  tab  bar  controller    tabBarController.viewControllers  =  [NSArray  arrayWithObjects:                        navController,                        anotherNavController,                        someViewController,                        nil];

Page 51: 08_NavigationTabBarController

Table Views

•  Display  lists  of  content  – Single  column,  mul)ple  rows  – Ver)cal  scrolling  – Large  data  sets  

•  Powerful  and  ubiquitous  in  iPhone  applica)ons  

Page 52: 08_NavigationTabBarController

Table View Styles UITableViewStylePlain UITableViewStyleGrouped

Page 53: 08_NavigationTabBarController

Table View Anatomy Plain Style

Table Header

Section

Table Footer

Section Header

Table Cell

Section Footer

Page 54: 08_NavigationTabBarController

Table View Anatomy Grouped Style

Table Header

Section

Table Footer

Section Header

Table Cell

Section Footer

Page 55: 08_NavigationTabBarController

 Header  and  footer  of  a  sec)on  

Page 56: 08_NavigationTabBarController

UITabelView  

•  Declaring  methods  that  allow  you  to  configure  the  appearance  of  the  table  view  –  specifying  the  default  height  of  rows  or  providing  a  view  used  as  the  header  for  the  table  

–  access  to  the  currently  selected  row  as  well  as  specific  rows  or  cells  

–  manage  selec)ons,  scroll  the  table  view,  and  insert  or  delete  rows  and  sec)ons.    

•  UITableView  inherits  from  UIScrollView,  which  defines  scrolling  behavior  for  views  with  content  larger  than  the  size  of  the  window.    –  UITableView  redefines  the  scrolling  behavior  to  allow  ver)cal  scrolling  only.  

Page 57: 08_NavigationTabBarController

Using Table Views

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

Page 58: 08_NavigationTabBarController

Displaying Data in a Table View

Page 59: 08_NavigationTabBarController

A Naïve Solution

•  Table views display a list of data, so use an array [myTableView setList:myListOfStuff];

•  Issues with this approach

– All data is loaded upfront – All data stays in memory

Page 60: 08_NavigationTabBarController

A More Flexible Solution

•  Another object provides data to the table view – Not all at once – Just as it’s needed for display

•  Like a delegate, but purely data-oriented

Page 61: 08_NavigationTabBarController

Calling  sequence  for  crea)ng  and  configuring  a  table  view  

Page 62: 08_NavigationTabBarController

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;

Page 63: 08_NavigationTabBarController

Datasource Message Flow numberOfSectionsInTableView: tableView:numberOfRowsInSec)on:  tableView:cellForRowAtIndexPath:  

Page 64: 08_NavigationTabBarController

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

Page 65: 08_NavigationTabBarController

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

–  Section index + row index

•  Category on NSIndexPath with helper methods

@interface NSIndexPath (UITableView)

+ (NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

@property(nonatomic,readonly) NSUInteger section; @property(nonatomic,readonly) NSUInteger row;

@end

Page 66: 08_NavigationTabBarController

Crea&ng  a  Table  View  Programma&cally    @interface RootViewController : UIViewController <UITableViewDelegate,

UITableViewDataSource> { NSArray *timeZoneNames; } @property (nonatomic, retain) NSArray *timeZoneNames; @end

- (void)loadView { UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; tableView.delegate = self; tableView.dataSource = self; [tableView reloadData]; self.view = tableView; [tableView release]; }

Page 67: 08_NavigationTabBarController

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

Page 68: 08_NavigationTabBarController

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

new cell each time. - (UITableViewCell *)dequeueReusableCellWithIdentifier: (NSString *)identifier; - (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; }

Page 69: 08_NavigationTabBarController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [regions count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Number of rows is the number of time zones in the region for the specified section. Region *region = [regions objectAtIndex:section]; return [region.timeZoneWrappers count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // The header for the section is the region name -- get this from the region at the section index. Region *region = [regions objectAtIndex:section]; return [region name]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier”; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; } Region *region = [regions objectAtIndex:indexPath.section]; TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row]; cell.textLabel.text = timeZoneWrapper.localeName; return cell; }

Page 70: 08_NavigationTabBarController

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

–  When a row becomes visible –  When an update is explicitly requested by calling –

reloadData

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

[self.tableView reloadData]; }

Page 71: 08_NavigationTabBarController

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;

Page 72: 08_NavigationTabBarController

Calling  sequence  for  inser)ng  or  dele)ng  rows  in  a  table  view  

Page 73: 08_NavigationTabBarController

Mapping  levels  of  the  data  model  to  table  views  

Page 74: 08_NavigationTabBarController

Additional Datasource Methods

•  Titles for section headers and footers •  Allow editing and reordering cells

Page 75: 08_NavigationTabBarController

Appearance & Behavior

Page 76: 08_NavigationTabBarController

UITableView Delegate

•  Customize appearance and behavior •  Keep application logic separate from

view •  Often the same object as datasource

Page 77: 08_NavigationTabBarController

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;

Page 78: 08_NavigationTabBarController

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

Page 79: 08_NavigationTabBarController

Persistent Selection

Page 80: 08_NavigationTabBarController

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

Page 81: 08_NavigationTabBarController

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

// Don’t allow selecting certain rows? if (indexPath.row == ...) { return nil; } else { return indexPath; }

}

Page 82: 08_NavigationTabBarController

UITableViewController

Page 83: 08_NavigationTabBarController

UITableViewController •  Convenient starting point for view controller

with a table view – Table view is automatically created – Controller is table view’s delegate and

datasource

•  Takes care of some default behaviors – Calls -reloadData the first time it appears – Deselects rows when user navigates back – Flashes scroll indicators

Page 84: 08_NavigationTabBarController

Table View Cells

Page 85: 08_NavigationTabBarController

Designated Initializer

   -­‐  (id)initWithFrame:(CGRect)frame      reuseIden)fier:(NSString  *)reuseIden)fier;  DEPRECATED

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

Page 86: 08_NavigationTabBarController

Cell Styles UITableViewCellStyleDefault UITableViewCellStyleSubtitle UITableViewCellStyleValue1 UITableViewCellStyleValue2

Page 87: 08_NavigationTabBarController

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

Page 88: 08_NavigationTabBarController

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

}

Page 89: 08_NavigationTabBarController

Customizing the Content View •  For cases where a simple image + text cell doesn’t suffice •  UITableViewCell has a content view property

–  Add additional views to the content view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = ...; CGRect frame = cell.contentView.bounds;

UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];

myLabel.text = ...; [cell.contentView addSubview:myLabel];

[myLabel release]; }

Page 90: 08_NavigationTabBarController

•  Ques)ons?