Advertisement
wkerswell

table view cell height

Jul 15th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3.    
  4.    
  5.     static NSString *CellIdentifier = @"Cell";
  6.     //NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  7.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  8.     if (cell == nil)
  9.     {
  10.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  11.     }
  12.    
  13.     for(UIView *view in cell.contentView.subviews)
  14.     {
  15.         [view removeFromSuperview];
  16.     }
  17.    
  18.     noteBody = [Noifications objectAtIndex:indexPath.row];
  19.     yPosition = 10;
  20.    
  21.    
  22.     UILabel *Title = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-20, 0)];
  23.     Title.text = [noteBody objectForKey:@"subject"];
  24.     Title.font = [UIFont boldSystemFontOfSize:20];
  25.     Title.numberOfLines = 0;
  26.     [Title sizeToFit];
  27.     yPosition += Title.frame.size.height;
  28.     [cell.contentView addSubview:Title];
  29.    
  30.     UILabel *Date = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-20, 25)];
  31.     Date.text = [noteBody objectForKey:@"datesent"];
  32.     Date.numberOfLines = 0;
  33.     Date.textColor = [UIColor grayColor];
  34.     [Title sizeToFit];
  35.     yPosition += Date.frame.size.height;
  36.     [cell.contentView addSubview:Date];
  37.  
  38.    
  39.     UILabel *Body = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-20, 0)];
  40.     Body.text = [noteBody objectForKey:@"body"];
  41.     Body.numberOfLines = 0;
  42.     [Body sizeToFit];
  43.     yPosition += Body.frame.size.height;
  44.     [cell.contentView addSubview:Body];
  45.  
  46.  
  47.    
  48.     return cell;
  49. }
  50.  
  51.  
  52.  
  53. #pragma mark - Table view delegate
  54.  
  55.  
  56. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  57. {
  58.     noteBodyCellHeight = [Noifications objectAtIndex:indexPath.row];
  59.    
  60.     NSString *title = [noteBodyCellHeight objectForKey:@"subject"];
  61.     NSString *date = [noteBodyCellHeight objectForKey:@"datesent"];
  62.     NSString *body = [noteBodyCellHeight objectForKey:@"body"];
  63.    
  64.     CGRect frame = [UIScreen mainScreen].bounds;
  65.     CGFloat width = frame.size.width;
  66.    
  67.     int section = indexPath.section;
  68.    
  69.    
  70.     CGSize title_size = {0, 0};
  71.     CGSize date_size = {0, 0};
  72.     CGSize body_size = {0, 0};
  73.    
  74.     if (title && [title isEqualToString:@""] == NO ) {
  75.         title_size = [title sizeWithFont:[UIFont systemFontOfSize:22.0]
  76.                               constrainedToSize:CGSizeMake(width, 4000)
  77.                                   lineBreakMode:UILineBreakModeWordWrap];
  78.     }
  79.    
  80.     if (date && [date isEqualToString:@""] == NO ) {
  81.         date_size = [date sizeWithFont:[UIFont systemFontOfSize:18.0]
  82.                        constrainedToSize:CGSizeMake(width, 4000)
  83.                            lineBreakMode:UILineBreakModeWordWrap];
  84.     }
  85.    
  86.     if (body && [body isEqualToString:@""] == NO ) {
  87.         body_size = [body sizeWithFont:[UIFont systemFontOfSize:18.0]
  88.                                 constrainedToSize:CGSizeMake(width, 4000)
  89.                                     lineBreakMode:UILineBreakModeWordWrap];
  90.     }
  91.    
  92.     CGFloat title_height = title_size.height;
  93.     CGFloat date_height = date_size.height;
  94.     CGFloat body_height = body_size.height;
  95.    
  96.     //CGFloat content_size = Body.frame.size.height + Title.frame.size.height + Date.frame.size.height;
  97.     CGFloat content_size = title_height + date_height + body_height +25;
  98.     CGFloat height;
  99.    
  100.     switch ( section ) {
  101.            
  102.         case 0:
  103.             height = content_size;
  104.             break;
  105.            
  106.             //Just in case
  107.         default:
  108.             height = 44.0;
  109.             break;
  110.            
  111.     }
  112.    
  113.     return height;
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement