Balaji Kondalrayal
Balaji Kondalrayal

Reputation: 1751

Cell height not working properly

I used the following code to change the cell height dynamically. But it does not show anything. Can anyone help me to do that.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!self.customCell){
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    }

    //Cell configuration
    self.customCell.title.text = vendor[indexPath.row];

    int quoteIndex = indexPath.row % [vendor count];
    self.customCell.description.text = message[quoteIndex];

    //Cell Layout
    [self.customCell layoutIfNeeded];

    //Height of cell
    CGFloat height = [self.customCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    //    CGFloat height = 240;

    return height;
}

Upvotes: 0

Views: 261

Answers (2)

Jasmin
Jasmin

Reputation: 794

You are using same reference to refer to cell but you have to create seperate cell reference inside like following:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     static CustomCell *customCell = nil;
     static dispatch_once_t onceToken;
    dispatch_once(&onceToken, {
           customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
        });

// Rest of your code

    }

Upvotes: 0

Manann Sseth
Manann Sseth

Reputation: 2745

In custom cell, Description label - set number of lines = 0

[Lable SizeToFit] will help you.
This code may be help you.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!self.customCell){
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    }

    //Cell configuration
    self.customCell.title.text = vendor[indexPath.row];

    int quoteIndex = indexPath.row % [vendor count];
    self.customCell.description.text = message[quoteIndex];

    [self.customCell.description sizeToFit];

    float height = (CGRectGetMaxY(self.customCell.description.frame) + 5);
    return height;
}

Thanks

Upvotes: 1

Related Questions