Muhammad Salman
Muhammad Salman

Reputation: 25

Two Custom tableviewcell height on the basis of content

I have seen many questions and answers about this question but nothing is working. I have two custom Cell in a tablview. I want to change the height of cell on the basis of content.I am try to use this code right now but its never work.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell;
    NSLog(@"%ld",(long)indexPath.row);
    NSLog(@"%ld",(long)hospital.hospComment.count);
    if (hospital.hospComment.count > (indexPath.row+1)) {
        cell.personComment.text = [hospital.hospComment objectAtIndex:(indexPath.row +1)];

    }

    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    height += 1;

    return height;
}

How I can achieve that ??

Upvotes: 1

Views: 3989

Answers (4)

Koushik
Koushik

Reputation: 1250

I just created a simple project and faced the same problem, so I kept a method for calculating the height of cell in customTavleViewCell, here is what I did lets keep the tableview as tableview.h and tableview.m files and CustomTableViewCell.h and CustomTableViewCell.m In tableview.h

@property (nonatomic,strong) UIViewController *tempViewController;

In tableview.h (heightforrowatindex path)

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [[CustomTableViewCell alloc]init];
_tempViewController = [[UIViewController alloc]init];
[_tempViewController.view addSubview:cell];
if(hospital.hospComment.count > (indexPath.row+1)) {
{
    cell.personComment.text = [hospital.hospComment objectAtIndex:(indexPath.row +1)];
}return [cell calcheight];}

in CustomTableViewCell.h

-(CGFloat)calcheight;

in CustomTableViewCell.m

-(CGFloat)calcheight{
CGFloat height;
// do calculations using the text in personComment
return height;}

And if you have any problem or have any doubts comment below

Kind Regards

Koushik S

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27448

You should declare cell like,

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCellMain"];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableViewCellMain"];

}

Accordint to your declaration cell will be nil. second thing make sure you are giving proper constraints

And another suggestion : you can use apple's new feature UITableViewAutomaticDimension and estimatedRowHeight. To know more about it refer this greate and little tutorial from Appcoda.

Hope this will help :)

Upvotes: 0

Arasuvel
Arasuvel

Reputation: 3012

If you are using supporting iOS 8 and above there is no need to calculate height for each row, if you have set the constraints in your cell correctly.

You need include the following lines in viewDidLoad :

[self.tableView setRowHeight:UITableViewAutomaticDimension];
[self.tableView setEstimatedRowHeight:45.0];

If you are supporting versions less iOS 8, you need to calculate the height manually for each row. You have plenty of tutorial for calculating the height of cell.

Upvotes: 0

suhayl
suhayl

Reputation: 11

Did you cell have right constraints ? If not , this code will never work.Because apple cannot caculate your cell height

Upvotes: 1

Related Questions