I am posting the sample code that will expand cell based on button click and the text size works for both iOS6 and iOS 7, this is just the sample code, just go through this this may helps u ... :)
this is just a sample project that u can try
in customCell.h
#import <UIKit/UIKit.h>
@class CustomCell;
@protocol ButtonClickDelegate <NSObject> //custom delegate
- (void)whenReadMoreButtonClicked:(CustomCell *)cell;//i am passing the cell itself
@end
@interface CustomCell : UITableViewCell
@property (nonatomic,assign)id<ButtonClickDelegate>delegate;
@property (nonatomic,retain)UILabel *mesageLabel;
@property (nonatomic,retain)NSString *message;
@property (nonatomic,assign)BOOL expand;
@end
in customCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize delegate;//synthesize it
@synthesize mesageLabel;
@synthesize message;
@synthesize expand;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(5,2, 100, 35)];
[button addTarget:self action:@selector(whenButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Read More" forState:UIControlStateNormal];
button.backgroundColor = [UIColor greenColor];
self.mesageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0 , 40,0 ,0)];
self.mesageLabel.backgroundColor = [UIColor redColor];
self.mesageLabel.numberOfLines = 100;
[self addSubview:self.mesageLabel];
[self addSubview:button];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)whenButtonClicked:(id)sender
{
if([self.delegate respondsToSelector:@selector(whenReadMoreButtonClicked:)])
{
[self.delegate whenReadMoreButtonClicked:self];//delegate to controller
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.mesageLabel.text = self.message;
if(self.expand)
{
CGSize size = [self findMessgeStringHeight];
self.mesageLabel.frame = CGRectMake(0, 40, size.width, size.height);
}
else
{
self.mesageLabel.frame = CGRectMake(0, 40, self.bounds.size.width, 100);
}
}
//helper method to find height
- (CGSize)findMessgeStringHeight
{
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.message attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f] }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize requiredSize = rect.size;
return requiredSize; //finally u return your height
}
@end
in viewController
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ( <UITableViewDataSource,UITableViewDelegate,ButtonClickDelegate>//confirm's to delegate
{
BOOL ButtonClickedForExpand;
NSMutableArray *array;
}
@property (nonatomic,retain)NSIndexPath *previousIndexPath;
@property (nonatomic,retain)NSIndexPath *currentIndexPath;
@end
@implementation ViewController
@synthesize previousIndexPath;
@synthesize currentIndexPath;
- (void)viewDidLoad
{
[super viewDidLoad];
ButtonClickedForExpand = NO;
// Do any additional setup after loading the view, typically from a nib.
array = [[NSMutableArray alloc]initWithObjects:@"hello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext",@"some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext",@"ello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext ello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext ello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext ello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext ello happy coding some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtext some longtextsome longtext some longtext", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
if(ButtonClickedForExpand)
{
if(indexPath.row == currentIndexPath.row)
{
cell.expand = YES;
}
else
{
cell.expand = NO;
}
}
else
{
cell.expand = NO;
}
cell.message = [array objectAtIndex:indexPath.row];
cell.delegate = self;//u need to set delegate to self
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = [self findMessgeStringHeight:[array objectAtIndex:indexPath.row]];
if(ButtonClickedForExpand)
{
if(indexPath.row == currentIndexPath.row)
{
return size.height + 30;
}
else
{
return 100;//by default
}
}
else
{
return 100;
}
}
//helper function to return the correct height for your label
- (CGSize)findMessgeStringHeight:(NSString *)str
{
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:str attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f] }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize requiredSize = rect.size;
return requiredSize; //finally u return your height
}
- (void)whenReadMoreButtonClicked:(CustomCell *)cell
{
ButtonClickedForExpand = YES;
self.previousIndexPath = self.currentIndexPath;
self.currentIndexPath = [self.tableView indexPathForCell:cell];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.currentIndexPath] withRowAnimation:UITableViewRowAnimationFade];
if(self.previousIndexPath.row == nil)
{
return;
}
else
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:self.previousIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
@end
EDIT:ADDED ButtonClickedForExpand
to for first click
EDIT:2 changed if(self.previousIndexPath.row == nil)
in "whenReadMoreButtonClicked" method of view controller
Comment if u don't get