24
1 - CSE 436 – Software Engineering Workshop Announcements Lab 3 is due on Wednesday at 11:59 PM Discuss So;ware Engineering Topics in the next two lectures Midterm is on Wednesday October 7th 2 - CSE 436 – Software Engineering Workshop iOS Development Opportunity WashU Alum Elizabeth Russell is looking for a partNme iOS developer for local startup Mindset – www.mindsetapp.com Feel free to contact her if interested [email protected]

cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 1 1 - CSE 436 – Software Engineering Workshop

Announcements  

•  Lab  3  is  due  on  Wednesday  at  11:59  PM    •  Discuss  So;ware  Engineering  Topics  in  the  next  two  lectures  

•  Midterm  is  on  Wednesday  October  7th  

Extensible Networking Platform 2 2 - CSE 436 – Software Engineering Workshop

iOS  Development  Opportunity  

• WashU  Alum  Elizabeth  Russell  is  looking  for  a  part-­‐Nme  iOS  developer  for  local  startup  Mindset    – www.mindset-­‐app.com    

•  Feel  free  to  contact  her  if  interested  –  [email protected]    

Page 2: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 3 3 - CSE 436 – Software Engineering Workshop

Today’s  Topics  

•  Scroll  views    

•  Table  views  – Displaying  data    –  Controlling  appearance  &  behavior  

•  UITableViewController    

•  Table  view  cells  

Extensible Networking Platform 4 4 - CSE 436 – Software Engineering Workshop

Scroll  Views  

Page 3: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 5 5 - CSE 436 – Software Engineering Workshop

UIScrollView  

•  For  displaying  more  content  than  can  fit  on  the  screen    

•  Handles  gestures  for  panning  and  zooming      

•  Noteworthy  subclasses:  UITableView  and  UITextView  

Extensible Networking Platform 6 6 - CSE 436 – Software Engineering Workshop

Scrolling  Examples  

Page 4: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 7 7 - CSE 436 – Software Engineering Workshop

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  frame)  –  frame  =  CGRectMake(0,  0,  500,  500);  –   myImageView  =  [[UIImageView  alloc]  initWithFrame:frame];    

–  [scrollView  addSubview:myImageView];    

•  Set  the  content  –   sizescrollView.contentSize  =  CGSizeMake(500,  500);  

Extensible Networking Platform 8 8 - CSE 436 – Software Engineering Workshop

Frame  and  Content  

scrollView.frame

Page 5: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop

Frame  and  Content  

scrollView.contentSize

Extensible Networking Platform 10 10 - CSE 436 – Software Engineering Workshop

Frame  and  Content  

scrollView.contentOffset

Page 6: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 11 11 - CSE 436 – Software Engineering Workshop

Demo    

                                                                 Using  a  UIScrollView  

Extensible Networking Platform 12 12 - CSE 436 – Software Engineering Workshop

Extending  Scroll  View  Behavior  

•  ApplicaNons  o;en  want  to  know  about  scroll  events      – When  the  scroll  offset  is  changed  – When  dragging  begins  &  ends  – When  deceleraVon  begins  &  ends  

Page 7: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 13 13 - CSE 436 – Software Engineering Workshop

Extending  with  a  Subclass  

•  Create  a  subclass    •  Override  methods  to  customize  behavior  •  Issues  with  this  approach    

– ApplicaVon  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  Vghtly  coupled  with  superclass  

Extensible Networking Platform 14 14 - CSE 436 – Software Engineering Workshop

Extending  with  DelegaNon  

•   Delegate  is  a  separate  object    

•  Clearly  defined  points  of  responsibility  –  Change  behavior    –  Customize  appearance  

•  Loosely  coupled  with  the  object  being  extended  

Page 8: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 15 15 - CSE 436 – Software Engineering Workshop

UIScrollView  Delegate  @protocol  UIScrollViewDelegate<NSObject>  

@opNonal  

//  Respond  to  interesNng  events  -­‐  (void)scrollViewDidScroll:(UIScrollView  *)scrollView;  

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

 @end  

Extensible Networking Platform 16 16 - CSE 436 – Software Engineering Workshop

ImplemenNng  a  Delegate  

•  Conform  to  the  delegate  protocol          @interface  MyController  :  NSObject  <UIScrollViewDelegate>      

•  Implement  all  required  methods  and  any  opNonal  methods  

       -­‐(void)scrollViewDidScroll:(UIScrollView  *)scrollView          {            //  Do  something  in  response  to  the  new  scroll  posiNon                if  (scrollView.contentOffset  ...)  {                              }          }  

Page 9: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 17 17 - CSE 436 – Software Engineering Workshop

Zooming  with  a  Scroll  View  

•  Set  the  minimum,  maximum,  iniNal  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;  }  

Extensible Networking Platform 18 18 - CSE 436 – Software Engineering Workshop

Demo  Zooming  with  a  UIScrollView  

Page 10: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 19 19 - CSE 436 – Software Engineering Workshop

Table  Views  

Extensible Networking Platform 20 20 - CSE 436 – Software Engineering Workshop

Table  Views  

•  Display  lists  of  content  –  Single  column,  mulVple  rows  – VerVcal  scrolling  –  Large  data  sets  

•  Powerful  and  ubiquitous  in  iPhone  applicaNons  

Page 11: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 21 21 - CSE 436 – Software Engineering Workshop

Table  View  Styles  UITableViewStylePlain   UITableViewStyleGrouped  

Extensible Networking Platform 22 22 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

Table  Header  -­‐>  

Page 12: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 23 23 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

SecNon  Header  -­‐>  

Extensible Networking Platform 24 24 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

Table  Cell-­‐>  

Page 13: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 25 25 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

SecNon  Footer  -­‐>  

Extensible Networking Platform 26 26 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

SecNon  -­‐>  

Page 14: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 27 27 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

Table  Footer-­‐>  

Extensible Networking Platform 28 28 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Plain  Style  

Table  Cell-­‐>  SecNon  Footer  -­‐>  

SecNon  Header  -­‐>  

SecNon  -­‐>  

Table  Footer-­‐>  

Table  Header  -­‐>  

Page 15: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 29 29 - CSE 436 – Software Engineering Workshop

Table  View  Anatomy  –  Grouped  Style  

Table  Cell-­‐>  SecNon  Footer  -­‐>  

SecNon  Header  -­‐>  

SecNon  -­‐>  

Table  Footer-­‐>  

Table  Header  -­‐>  

Extensible Networking Platform 30 30 - CSE 436 – Software Engineering Workshop

Displaying  Data  in  a  Table  View  

Page 16: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 31 31 - CSE 436 – Software Engineering Workshop

A  Naïve  SoluNon  

•  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  

Extensible Networking Platform 32 32 - CSE 436 – Software Engineering Workshop

A  More  Flexible  SoluNon  

•  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 17: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 33 33 - CSE 436 – Software Engineering Workshop

UITableViewDataSource  

•  Provide  number  of  secNons  and  rows  

       //  OpNonal  method,  defaults  to  1  if  not  implemented      -­‐(NSInteger)numberOfSecNonsInTableView:(UITableView  *)table;                //  Required  method      -­‐  (NSInteger)tableView:(UITableView  *)tableView        numberOfRowsInSecNon:(NSInteger)secNon;  

 

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

Extensible Networking Platform 34 34 - CSE 436 – Software Engineering Workshop

Datasource  Message  Flow  

numberOfSecNonsInTableView:  

Datasource

How many Sections?

5

Page 18: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 35 35 - CSE 436 – Software Engineering Workshop

Datasource  Message  Flow  

tableView:numberOfRowsInSecNon:    

Datasource

How many rows in Section 0?

1

Extensible Networking Platform 36 36 - CSE 436 – Software Engineering Workshop

Datasource  Message  Flow  

tableView:cellForRowAtIndexPath:    

Datasource

What to display at section 0, row 0?

“John Appleseed”

Page 19: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 37 37 - CSE 436 – Software Engineering Workshop

NSIndexPath  

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

Extensible Networking Platform 38 38 - CSE 436 – Software Engineering Workshop

NSIndexPath  and  Table  Views  

•  Cell  locaNon  described  with  an  index  path  –  SecVon  index  +  row  index  

•  Category  on  NSIndexPath  with  helper  methods        @interface  NSIndexPath  (UITableView)      +  (NSIndexPath      *)indexPathForRow:(NSUInteger)row                                                                  inSecNon:(NSUInteger)secNon;      @property(nonatomic,readonly)  NSUInteger  secNon;    @property(nonatomic,readonly)  NSUInteger  row;      @end  

Page 20: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 39 39 - CSE 436 – Software Engineering Workshop

Single  SecNon  Table  View  

Return  the  number  of  rows  -­‐  (NSInteger)tableView:(UITableView  *)tableView      numberOfRowsInSecNon:(NSInteger)secNon  {          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];  }    

Extensible Networking Platform 40 40 - CSE 436 – Software Engineering Workshop

Empty  ApplicaNon  Demo  

Page 21: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 41 41 - CSE 436 – Software Engineering Workshop

Cell  Reuse  

• When  asked  for  a  cell,  it  would  be  expensive  to  create  a  new  cell  each  Nme.  

-­‐  (UITableViewCell  *)tableView:(UITableView  *)tableView  cellForRowAtIndexPath:(NSIndexPath  *)indexPath  {        UITableViewCell  *cell  =  [tableView        dequeueReusableCellWithIdenNfier:@”MyIdenNfier”];          if  (cell  ==  nil)  {            cell  =  [[[UITableViewCell  alloc]            initWithFrame:...  reuseIdenNfier:@”MyIdenifier”];        }  cell.textLabel.text  =  [myStrings  objectAtIndex:indexPath.row]  return  cell;  }  

Extensible Networking Platform 42 42 - CSE 436 – Software Engineering Workshop

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 22: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 43 43 - CSE 436 – Software Engineering Workshop

Simple  Demo  and  Flickr  Demo  

Extensible Networking Platform 44 44 - CSE 436 – Software Engineering Workshop

Basic  properNes  

•  UITableViewCell  has  image  and  text  properNes                    cell.image  =  [UIImage  imageNamed:@“obama.png”];          cell.textLabel.text  =  @“Barack  Obama”;  

Page 23: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 45 45 - CSE 436 – Software Engineering Workshop

Accessory  Types    //  UITableView  delegate  method  (UITableViewCellAccessoryType)tableView:(UITableView  *)table  

accessoryTypeForRowWithIndexPath:(NSIndexPath  *)indexPath    

return  UITableViewCellAccessoryDisclosureIndicator    return  UITableViewCellAccessoryDetailDisclosureBuron    return  UITableViewCellAccessoryCheckmark          

-­‐  (void)tableView:(UITableView  *)tableView  accessoryBuronTappedForRowWithIndexPath:(NSIndexPath  *)indexPath  {  

 //  Only  for  the  blue  disclosure  buron    NSUInteger  row  =  indexPath.row;  

...  

Extensible Networking Platform 46 46 - CSE 436 – Software Engineering Workshop

Customizing  the  Content  View  

•  For  cases  where  a  simple  image  +  text  cell  doesn’t  suffice  

•  UITableViewCell  has  a  content  view  property  –  Add  addiVonal  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 24: cse436 lecture8 9-28-15todd/cse436/cse436_lecture8_9-28-15.pdf · Extensible Networking Platform 9 9 - CSE 436 – Software Engineering Workshop Frame*and*Content* scrollView.contentSize

Extensible Networking Platform 47 47 - CSE 436 – Software Engineering Workshop

Custom  Row  Heights  •  Rows  in  a  table  view  may  have  variable  heights  

•  NSString  category  in  UIStringDrawing.h  is  very  useful  for  compuNng  text  sizes  

-­‐  (CGFloat)tableView:(UITableView  *)tableView  heightForRowAtIndexPath:(NSIndexPath  *)indexPath  {  

           NSString  *text  =  ...;  

 UIFont  *font  =  [UIFont  systemFontOfSize:...];    

 CGSize  withinSize  =  CGSizeMake(tableView.width,  1000];    

 CGSize  size  =  [text  sizeWithFont:font                          constrainedToSize:withinSize        lineBreakMode:UILineBreakModeWordWrap];  

       return  size.height  +  somePadding;  }  

Extensible Networking Platform 48 48 - CSE 436 – Software Engineering Workshop

Custom  Cells  Demo