I am trying to make an offscreen version of a cell that will later be rendered and then use
[cell.label sizeToFit];
then use the new adjusted size of the label to calculate a height like this:
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
However, the problem I am having is that for height I always get 0 and I am not sure why.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Make an off screen cell
ZSSComplishTableViewCell *cell = [[ZSSComplishTableViewCell alloc] init];
//Which section?
if (indexPath.section == 0) {
//Get a statement from the datasource and assign it to the label
NSArray *todayComplishs = [[ZSSComplishStore sharedStore] todayComplishs];
ZSSComplish *complish = todayComplishs[indexPath.row];
cell.complishLabel.text = complish.statement;
//Resize label to the amount of text
[cell.complishLabel sizeToFit];
NSLog(@"calculated height of label: %f", cell.complishLabel.frame.size.height);
//Get height of label
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
} else if (indexPath.section == 1) {
//Get a statement from the datasource and assign it to the label
NSArray *tomorrowComplishs = [[ZSSComplishStore sharedStore] tomorrowComplishs];
ZSSComplish *complish = tomorrowComplishs[indexPath.row];
cell.complishLabel.text = complish.statement;
//Resize label to the amount of text
[cell.complishLabel sizeToFit];
//Get height of label
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
}
return 100;
}
This is the ZSSComplishTableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}