Advertisement
Guest User

resizing table example

a guest
May 18th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  tableFooterUpdateFail
  4. //
  5. //  Created by Dirk-Willem van Gulik on 16-05-12.
  6. //  Copyright (c) 2012 Dirk-Willem van Gulik. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController
  16. @synthesize items;
  17.  
  18. - (void)viewDidLoad
  19. {
  20.     [super viewDidLoad];
  21.     items = [NSMutableArray arrayWithObjects:@"Ape",@"Nut",
  22.              @"Denkend aan Holland, zie ik breede rivieren, traag door oneindig, laagland gaan, rijen ondenkbaar ijle populieren als hooge pluimen aan den einder staan.",
  23.              @"Mies",@"Fish",
  24.              @"Denkend aan Holland, zie ik breede rivieren, traag door oneindig, laagland gaan, rijen ondenkbaar ijle populieren als hooge pluimen aan den.",
  25.              @"Denkend aan Holland, zie ik breede rivieren, traag door oneindig, laagland gaan.",
  26.              @"Denkend aan Holland, zie ik breede rivieren, traag door oneindig, laagland gaan, rijen ondenkbaar ijle populieren als hooge pluimen aan den einder staan. Denkend aan Holland, zie ik breede rivieren, traag door oneindig, laagland gaan, rijen ondenkbaar ijle populieren als hooge pluimen aan den einder staan.",
  27.              
  28.              nil];
  29.     UIBarButtonItem * editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
  30.                                                                                  target:self
  31.                                                                                  action:@selector(editList:)];
  32.    
  33.     [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:editButton, nil]];    
  34. }
  35.  
  36. -(IBAction)editList:(id)sender {
  37.     BOOL isEditing = ![self isEditing];
  38.     [self setEditing:isEditing animated:YES];
  39.     [[[self.navigationItem rightBarButtonItems] lastObject] setTintColor:isEditing ? [UIColor redColor] : nil];
  40. }
  41.  
  42. #pragma mark - Table view data source
  43.  
  44. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  45. {
  46.     return 1;
  47. }
  48.  
  49. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  50. {
  51.     return [items count];
  52. }
  53.  
  54. -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  55.     NSString *text = [items objectAtIndex:[indexPath row]];
  56.    
  57.     CGSize constraint = CGSizeMake(self.view.bounds.size.width-24.f, CGFLOAT_MAX);
  58.    
  59.     UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  60.  
  61.     UIFont * font = cell.textLabel.font;
  62.     if (font.pointSize == 0)
  63.         font = [UIFont systemFontOfSize:20];
  64.        
  65.     CGSize size = [text sizeWithFont:font
  66.                    constrainedToSize:constraint
  67.                        lineBreakMode:UILineBreakModeWordWrap];
  68.        
  69.     CGFloat height = MAX(size.height + 12.f, tableView.rowHeight);
  70.    
  71.     return height;
  72. }
  73.  
  74. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  75. {
  76.     static NSString *CellIdentifier = @"Cell";
  77.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  78.     if (!cell)
  79.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  80.    
  81.     cell.textLabel.text = [items objectAtIndex:indexPath.row];
  82.     cell.textLabel.numberOfLines = 0; // whatever is needed to make this fit.
  83.     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
  84.     return cell;
  85. }
  86.  
  87. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  88. {
  89.     return YES;
  90. }
  91.  
  92. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94.     if (editingStyle == UITableViewCellEditingStyleDelete) {
  95.         [items removeObjectAtIndex:indexPath.row];
  96.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  97.                          withRowAnimation:UITableViewRowAnimationFade];
  98.  
  99.         // We'd hope that this would update the count.
  100.         //
  101.         [tableView reloadSectionIndexTitles];
  102.        
  103.         // We really would like to avoid this:
  104.         [tableView reloadData];
  105.     }
  106. }
  107.  
  108. #pragma mark - Table view delegate
  109.  
  110. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. }
  113.  
  114. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  115.     return [NSString stringWithFormat:@"We have %d item%@ above",
  116.             [items count], [items count] == 1 ? @"" : @"s"];
  117. }
  118. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement