0

My question is how can I adjust the large text and Fixed size of image in UITableViewCell.
I need the cell to be autoresize according to data like WhatsApp. How can i do it. My cell is resizing when I am using the UILabel but in case of image it messed up everything.

Please suggest me. Thanks in advance.

@ddb this is my source code I am using.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [adminDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"admincell";
    adminCell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if (adminCell==nil) {
        adminCell=[[AdminPostTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    adminCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [adminCell setBackgroundColor:[UIColor clearColor]];
    NSMutableDictionary *getAdminDataDictionary=[adminDataArray objectAtIndex:indexPath.row];
    if ([getAdminDataDictionary objectForKey:@"TEXT"])
    {
        adminCell.lblSenderAdminPage.text=[getAdminDataDictionary objectForKey:@"TEXT"];
        adminCell.lblSenderAdminPage.textColor=[UIColor blackColor];
        adminCell.lblSenderAdminPage.backgroundColor=[UIColor whiteColor];
        adminCell.lblSenderAdminPage.preferredMaxLayoutWidth=305;
        adminCell.lblSenderAdminPage.layer.masksToBounds=YES;
        adminCell.lblSenderAdminPage.layer.cornerRadius=5;
        adminCell.lblSenderAdminPage.layer.borderWidth=1.0f;
        adminCell.lblSenderAdminPage.layer.borderColor=[UIColor blackColor].CGColor;
    }
    else if ([getAdminDataDictionary objectForKey:@"IMAGE"]){        
        adminCell.imgSenderAdminPage.image=[getAdminDataDictionary objectForKey:@"IMAGE"];
    }
    return adminCell;
}
Abhishek Sharma
  • 73
  • 3
  • 10

1 Answers1

0

You can try using

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGSize messageSize  // Your message size
    if (img) {
        CGFloat imgHeight = img.size.height;  // Image size
        return messageSize.height + imgHeight + 2* textMarginVertical;
    } else {
        return messageSize.height + 2* textMarginVertical;
    }
}

In the top of the class declare

static CGFloat textMarginVertical = 7f;

Happy coding..

Sailendra
  • 1,318
  • 14
  • 29