Dynamically change UITable cell height?

后端 未结 5 1602
心在旅途
心在旅途 2021-01-01 05:44

I need to resize the cell height based on the content size/length.tried several methods, which one gives the exact height without overlapping?

5条回答
  •  萌比男神i
    2021-01-01 06:29

    Sample you can edit and try

     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath         *)indexPath {
    
    NSDictionary *itemAtIndex = (NSDictionary *)[chatQuestions objectAtIndex:indexPath.row];
    
    if ([self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20>50) {
        return [self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20;
    }
    else{
        return 50;}
    
    
    
    }
    
     -(CGSize)sizeForText:(NSString*)text
      {
    
    
    CGSize constraintSize;
    
    constraintSize.width = 190.0f;
    
    constraintSize.height = MAXFLOAT;
    UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Light" size:18];
    
    CGSize stringSize =[text sizeWithFont:labelFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
    
    return stringSize;
    }
    

提交回复
热议问题