I have a UITable that I am expanding to show “more info” text. Problem I have is the text can be varied sizes, i.e indexpath.section1 might be 1200 high where as index path.section2 is 300 high.
The code I have works fine until it comes to re-using the cell, the size of the label frame isnt adjusting for the new label. So although the cell is sized correctly the label inside that cell still has the original sized frame so if its too small the text gets cut off and I get loads of white space and if its too big then I cant see the text unless I scroll to the middle of the field.
What am I doing wrong?? any help please??
This is my code:-
static NSUInteger const k1 = 1;
// Declare references to the subviews which will display the data.
UILabel *lbl1 = nil;
static NSString *kCellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
if ((indexPath.section == 8) || (indexPath.section == 9) || (indexPath.section == 10) || (indexPath.section == 11) || (indexPath.section == 12) || (indexPath.section == 13) || (indexPath.section == 14)|| (indexPath.section == 15) || (indexPath.section == 16))
{
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 200)];
}
else if ((indexPath.section == 4) || (indexPath.section == 5))
{
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 1200)];
}
lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl1.tag = k1;
lbl1.textColor = [UIColor blackColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.highlightedTextColor = [UIColor whiteColor];
lbl1.opaque = NO;
lbl1.adjustsFontSizeToFitWidth = TRUE;
lbl1.numberOfLines = 0;
lbl1.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:lbl1];
} else {
lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
}
cell.accessoryView.tag = indexPath.row;
lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
return cell;
}
}
}
You need to set the autoresizing mask on your label. This will cause it to be resized when its superview is resized. The appropriate setting would be: