Reputation: 6490
I have implemented UITableView with Custom cell,
What i want to do is, i want to change height UITableViewCell
according to text length,(Dynamic height)
here is my code snippet,
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [DescArr objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 55.0);
return height;
}
Height of UITableViewCell
changes properly but the height of CustomCell does not change,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"BTSTicketsCellIdentifier";
CRIndCoupnCell *cell = (CRIndCoupnCell *)[tblDescriptn dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CRIndCoupnCell" owner:self options:nil];
for (id oneObject in nib) if ([oneObject isKindOfClass:[CRIndCoupnCell class]])
cell = (CRIndCoupnCell *)oneObject;
}
NSLog(@"cell.frame.size.height=%f",cell.frame.size.height);
cell.lblleft.text=[arr objectAtIndex:indexPath.row];
cell.lblRight.text=[DescArr objectAtIndex:indexPath.row];
return cell;
}
My Log shows cell.frame.size.height=100.0
in all the rows. height of CustomCell doesn't change.
:
Where am i making mistake ? please help.
Thanks in advance
Upvotes: 0
Views: 192
Reputation: 3117
You are constraining the string to size of the full width of cell minus 2 times the cell margin and a max height of 2000 but since you are setting the string as text of the label with green colored text color, you should rather check for the size according to that label's width and an arbitrary height of 2000 or anything of your choice.
CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000);
Upvotes: 1