Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Height For UITableViewCell and The content inside
    primarykey
    data
    text
    <p>I have a particularly problem that i can't seem to solve i have looked at my code for a few hours and can't seem to fix the problem. </p> <p>When i add messages to my tableview the messages overflows the wrapper within the UITableViewCell and sometimes the wrapper gets cutoff by the cell.</p> <p>How do i make sure that my wrapper is always big enough to hold the txt (lblMessage) and the cell is big enough to hold the wrapper. </p> <p>Thanks in advance !</p> <pre><code>-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGFloat height = [self heightForMessage:indexPath]; if(height &lt; 50) { NSLog(@"Normal : %d , row : %d", 100,indexPath.row); return 100; } else { NSLog(@"Custom : %f , row : %d", 70 + height,indexPath.row); return 70 + height; } } -(CGFloat)heightForMessage:(NSIndexPath *)indexPath { MessageObject * model = [self.chats objectAtIndex:indexPath.row]; CGSize size = [model.message sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12] constrainedToSize:CGSizeMake(290, 100000) lineBreakMode:NSLineBreakByWordWrapping]; return size.height; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * NewsCellIdentifer = @"NewsCellIdentifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifer]; UILabel * lblContact; UILabel * lblMessage; UILabel * lblDate; UIView * wrapper; if (cell == nil) { /* Top menu for login */ wrapper = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 90)]; [wrapper setBackgroundColor:[self.delegate.color colorWithAlphaComponent:0.6f]]; [wrapper.layer setCornerRadius:6.0f]; [wrapper.layer setShadowOffset:CGSizeMake(0, 2)]; [wrapper.layer setShadowRadius:2.0f]; [wrapper.layer setShadowOpacity:0.5f]; [wrapper viewWithTag:19]; cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NewsCellIdentifer]; /* Contact Name */ lblContact = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, 30)]; [lblContact setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:13]]; [lblContact setTag:13]; [lblContact setTextColor:[self.delegate colorFromHexString:@"#fffbff"]]; [lblContact setBackgroundColor:[UIColor clearColor]]; /* Received Message */ lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 50)]; [lblMessage setNumberOfLines:0]; [lblMessage setLineBreakMode:NSLineBreakByWordWrapping]; [lblMessage setBackgroundColor:[UIColor clearColor]]; [lblMessage setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12]]; [lblMessage setTextColor:[self.delegate colorFromHexString:@"#fffbff"]]; [lblMessage setTag:14]; /* Date received */ lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, 65, 30)]; [lblDate setText:@"4 hours ago"]; [lblDate setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11]]; [lblDate setTextColor:[self.delegate colorFromHexString:@"#fffbff"]]; [lblDate setTag:15]; /* Subview Logic */ [wrapper addSubview:lblContact]; [wrapper addSubview:lblMessage]; [wrapper addSubview:lblDate]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell.contentView addSubview:wrapper]; } else { lblContact = (UILabel *)[cell.contentView viewWithTag:13]; lblMessage = (UILabel *)[cell.contentView viewWithTag:14]; wrapper = [cell.contentView viewWithTag:19]; } MessageObject * model = [self.chats objectAtIndex:indexPath.row]; CGFloat height = [self heightForMessage:indexPath]; [lblMessage setFrame:CGRectMake(10, 25, 280, height)]; [lblMessage setText:model.message]; [lblContact setText:model.clientModel.firstName]; if(height &lt; 50) { NSLog(@"wrapper size : %d for row %d ", 90, indexPath.row); [wrapper setFrame:CGRectMake(10, 5, 300, 90)]; } else { NSLog(@"wrapper size : %f for row %d ", height + 60, indexPath.row); [wrapper setFrame:CGRectMake(10, 5, 300, height + 60)]; [lblDate setFrame:CGRectMake(10, height + 20, 65, 30)]; } [cell.contentView setBackgroundColor:[UIColor greenColor]]; [cell setBackgroundColor:[UIColor clearColor]]; return cell; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload