How to Increase Table Cell Size Dynamically in iPhone

Introduction

In this article I will create a Single View application. Here I will add a table view using outlet and connect it's delegate and datasource to the file owner. To increase the table cell size here we use the "-tableView heightForRowAtIndexPath:" method.

To better understand it we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on "Next".

Step 4

Now provide your Product Name and Company Identifier. 

Step 5

Select the location where you want to save your project and click on "Create".

Step 6

Now we write the code of the Objective-C classes:

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UITableView *table;
NSMutableArray *array;
}
@property(strong,nonatomic)IBOutlet UITableView *table;
@property(strong,nonatomic) NSMutableArray *array;
@end

TableExampleViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize table,array;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [[self table] setBackgroundColor:[UIColor yellowColor]];
array = [[NSMutableArray alloc]init];
[array addObject:@"Sun"];
[array addObject:@"Mon"];
[array addObject:@"Tues"];
[array addObject:@"Wed"];
[array addObject:@"Thur"];
[array addObject:@"Fri"];
[array addObject:@"Sat"];
[array addObject:@"Sun"];
[array addObject:@"Mon"];
[array addObject:@"Tues"];
[array addObject:@"Wed"];
[array addObject:@"Thur"];
[array addObject:@"Fri"];
[array addObject:@"Sat"];}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return 1;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#define FONT_SIZE 12.0f
//#define tableView.frame.size.width 320.0f
#define CELL_CONTENT_MARGIN 2.0f
//65 = image height if you have image in custom UITableViewCell
-(double)heightCalculate:(NSString *)calculateText{
UILabel *calculateText_lbl = [[UILabel alloc] init];
[calculateText_lbl setLineBreakMode:UILineBreakModeClip];
// [descrtiption_lbl setBackgroundColor:[UIColor grayColor]];
[calculateText_lbl setMinimumFontSize:FONT_SIZE];
[calculateText_lbl setNumberOfLines:0];
[calculateText_lbl setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];
[calculateText_lbl setFont:[UIFont systemFontOfSize:FONT_SIZE]];
NSString *text = calculateText;
CGSize constraint = CGSizeMake(self.view.frame.size.width - (CELL_CONTENT_MARGIN * 2), FLT_MAX);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[calculateText_lbl setText:text];
[calculateText_lbl setFrame:CGRectMake(65, CELL_CONTENT_MARGIN, ((self.view.frame.size.width - (CELL_CONTENT_MARGIN * 2)) -65),size.height)];
[calculateText_lbl sizeToFit];
double height_lbl = calculateText_lbl.frame.size.height;
return (height_lbl);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *userActivity = [NSString stringWithFormat:@"%@",[self.array objectAtIndex:indexPath.row ]];
NSString *text1 = [NSString stringWithFormat:@"Posted by %@ ",[self.array objectAtIndex:indexPath.row ]];
double _height = [self heightCalculate:userActivity];
double _height1 = [self heightCalculate:text1];
double height;
height= MAX((_height+_height1+15), 65.0f);
return (height + (CELL_CONTENT_MARGIN * 3));
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
@end

Step 7

Click on the "Run" button to show the output.

Output in iPhone:

Output-in-iPhone.png


Similar Articles