If you want to set different height for each row, you should use this method from UIViewDelegate Protocol:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This a sample for use it, code for ViewController.m:
#import "ViewController.h"
#import "MyTableCellTableViewCell.h"
@interface ViewController ()
@property NSMutableArray* dataArray;
@property UIFont* cellTextFont;
@end
NSString* cellID = @"MyTableViewCell";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_cellTextFont = [UIFont systemFontOfSize:15];
_dataArray = [[NSMutableArray alloc] initWithObjects:
@"Hello world!",
@"Hello world!Hello world!Hello world!",
@"Hello world!Hello world!Hello world!Hello world!Hello world!",
@"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
@"Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!Hello world!",
nil];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//Set different row height by Odd and even
// if (0 == indexPath.row % 2) {
// return 30;
// }
// else
// return 50;
//Set different row height by content text's height
NSString* contentStr = [_dataArray objectAtIndex:indexPath.row];
float textWidth = self.tableView.frame.size.width;
NSDictionary *attributes = @{NSFontAttributeName: _cellTextFont};
CGRect labelSize = [contentStr boundingRectWithSize:CGSizeMake(textWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
return labelSize.size.height;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (nil == cell) {
cell = [[MyTableCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.lbContent.font = _cellTextFont;
}
cell.lbContent.text = [_dataArray objectAtIndex:indexPath.row];
return cell;
}
@end
This is MyTableCellTableViewCell.h
#import <UIKit/UIKit.h>
@interface MyTableCellTableViewCell : UITableViewCell
@property UILabel* lbContent;
@end
And, this is MyTableCellTableViewCell.m:
@interface MyTableCellTableViewCell()
@property UIView* separation;
@end
@implementation MyTableCellTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_lbContent = [[UILabel alloc] init];
_lbContent.numberOfLines = INT_MAX;
[self addSubview:_lbContent];
_separation = [[UIView alloc] init];
_separation.backgroundColor = [UIColor blackColor];
[self addSubview:_separation];
return self;
}
-(void)layoutSubviews{
float separationHeight = 1;
_lbContent.frame = self.bounds;
_separation.frame = CGRectMake(0, self.bounds.size.height-separationHeight, self.bounds.size.width, separationHeight);
}
@end
Hope it can help you.