ݺߣ

ݺߣShare a Scribd company logo
Chapter 8
               Custom TableView Cell
                      Bit Academy
?
               ? NIB                   .

               ? NIB   Top Level           .

               ?                   .
?                          ,
                   ,                          .

               ?
                       .

               ?               UITableViewCell


                           .
IB



               NIB
NIB

?        File->New              IOS      User Interface               View
         Next          PhotoTableCell

?        PhotoTableCell.xib

?        PhotoTableCell.xib             IB      View              TableViewCell
                    TableViewCell                  UIImageView, UILabel
                               .

?        UIImageView                         Mode         Aspect To Fit
           .
?          IB

               ?                      IB       IBOutlet property


               ?   viewWithTag:

               ?   Tag            0        .
?   tableView: cellForRowAtIndexPath:
                           , PhotoTableCell.nib         tag


               ?   nib                   (
                      )

               ?                       [NSBundle mainBundle]
NSBundle Class
               ?   NSBundle
                                           .

               ?   mainBundle :
                   NSBundle                    . + (NSBundle *)mainBundle

               ?   - (NSArray *)loadNibNamed:(NSString *)name owner:
                   (id)owner options:(NSDictionary *)options

                   name                                       category NSBundle
                   nib            The name of the nib ?le, which need not include
                the .nib extension.
               owner :nib        File's Owner object.
               options : A dictionary containing the options to use when
          opening the nib ?le. For a list of available keys for this dictionary, see
          Nib File Loading Options. Nib                Toplevel
                             (                               root View 0
                .)
tableView: cellForRowAtIndexpath:
          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
              return [[self appDelegate].photoArray count];
          }

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

                   static NSString *CellIdentifier = @"Cell";

              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              if (cell == nil) {
                  //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
          reuseIdentifier:CellIdentifier] autorelease];
                  NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PhotoTableCell" owner:self
          options:nil];
                  cell = [topLevelObjects objectAtIndex:0]; //
              }

          !   NSDictionary *photoData = [[self appDelegate].photoArray objectAtIndex:indexPath.row];
          !
          !   // cell.textLabel.text = [photoData valueForKey:@"Country"];
          !   // cell.detailTextLabel.text = [photoData valueForKey:@"Region"];
          !   // cell.imageView.image = [photoData valueForKey:@"Thumbnail"];

                   UIImageView *imageView = (UIImageView *) [cell viewWithTag:1];
                   imageView.image = [photoData valueForKey:@"Thumbnail"];

                   UILabel *label;
                   label = (UILabel *) [cell viewWithTag:2];
                   label.text = [photoData valueForKey:@"Country"];

                   label = (UILabel *) [cell viewWithTag:3];
                   label.text = [photoData valueForKey:@"Region"];

          !   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                   return cell;
          }
?   RootViewController.xib   Tableview   Row
                   height PhotoTableCell.xib height
                        .

More Related Content

?????(6) pdf

  • 1. Chapter 8 Custom TableView Cell Bit Academy
  • 2. ? ? NIB . ? NIB Top Level . ? .
  • 3. ? , , . ? . ? UITableViewCell .
  • 4. IB NIB
  • 5. NIB ? File->New IOS User Interface View Next PhotoTableCell ? PhotoTableCell.xib ? PhotoTableCell.xib IB View TableViewCell TableViewCell UIImageView, UILabel . ? UIImageView Mode Aspect To Fit .
  • 6. ? IB ? IB IBOutlet property ? viewWithTag: ? Tag 0 .
  • 7. ? tableView: cellForRowAtIndexPath: , PhotoTableCell.nib tag ? nib ( ) ? [NSBundle mainBundle]
  • 8. NSBundle Class ? NSBundle . ? mainBundle : NSBundle . + (NSBundle *)mainBundle ? - (NSArray *)loadNibNamed:(NSString *)name owner: (id)owner options:(NSDictionary *)options name category NSBundle nib The name of the nib ?le, which need not include the .nib extension. owner :nib File's Owner object. options : A dictionary containing the options to use when opening the nib ?le. For a list of available keys for this dictionary, see Nib File Loading Options. Nib Toplevel ( root View 0 .)
  • 9. tableView: cellForRowAtIndexpath: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self appDelegate].photoArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PhotoTableCell" owner:self options:nil]; cell = [topLevelObjects objectAtIndex:0]; // } ! NSDictionary *photoData = [[self appDelegate].photoArray objectAtIndex:indexPath.row]; ! ! // cell.textLabel.text = [photoData valueForKey:@"Country"]; ! // cell.detailTextLabel.text = [photoData valueForKey:@"Region"]; ! // cell.imageView.image = [photoData valueForKey:@"Thumbnail"]; UIImageView *imageView = (UIImageView *) [cell viewWithTag:1]; imageView.image = [photoData valueForKey:@"Thumbnail"]; UILabel *label; label = (UILabel *) [cell viewWithTag:2]; label.text = [photoData valueForKey:@"Country"]; label = (UILabel *) [cell viewWithTag:3]; label.text = [photoData valueForKey:@"Region"]; ! cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }
  • 10. ? RootViewController.xib Tableview Row height PhotoTableCell.xib height .