0

how can i set different height for different rows of custom ui table view cell in ios? I am trying to change the height depending upon the how much lines there are in my uitextview which is inside my custom uitableview cell.

I tried setting height like this inside my heightForRowAtIndexPath method but it crashes:

PostStreamCell *cell = [tableView cellForRowAtIndexPath:indexPath];
int lines = cell.txtViewMessage.contentSize.height / cell.txtViewMessage.font.lineHeight;

if(lines < 4)
{
    return 100;
}
else if(lines == 4)
{
    return 100;
}
else{
    return 220;
}
Yogendra
  • 1,728
  • 16
  • 28
warzone_fz
  • 472
  • 1
  • 9
  • 24

4 Answers4

1

You can't use cellForRowAtIndexPath in heightForRowAtIndexPath because the cell does not exist. You must retrieve the text by another way without use the cell.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
tdelepine
  • 1,986
  • 1
  • 13
  • 19
0

Try by using the text boundingRectWithSize.. for calculating the size for cell itself .

Something like this :

 NSString *text = yourTextView.text;
    CGSize size;

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                          nil];
    CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(525, 500)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:attributesDictionary
                                             context:nil];
    size = fontSizeFor7.size;

    return size.height +40;
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0

I suggest you to count int lines in viewDidLoad method using length of your text field. Then store it in some array and use it values in your heightForRowAtIndexPath method. It should look like this

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (myLinesArray[indexPath.row] =< 4)
{
    return 100;
}
else return 220;
}
Victorianec
  • 587
  • 1
  • 6
  • 15
-1

You are setting the height correctly--however, you probably shouldn't retrieve a cell within heightForRowAtIndexPath by calling cellForRowAtIndexPath, that is unless your cells are already statically created. A bare-bones implementation of heightForRowAtIndexPath (below) doesn't crash, so the problem probably lies elsewhere in your view controller implementation, or elsewhere.

Also noticed that you are dividing by a dynamic value to get the height. Check your PostStreamCell--if the cell object, txtViewMessage, or its font property is nil, then you will be dividing by 0, and that could be causing the crash.

    //Very simple implementation to demo changes in height
    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row % 2) {
            return 60;
        } else {
            return 44;
        }
    }
Sheamus
  • 6,506
  • 3
  • 35
  • 61