1

I am trying to make a messaging app. I have a tableview where i have to add all the messages from JSON webservice. I am using a custom class inherited from UITalbeViewCell for custom cells.

I dont have any nib file with this custom class. I only make the object of this class and set its message label text and return as cell in "cellForRowAtIndexPath" delegate method.

Now i am facing a problem that custom class returns perfect layout message cell according to the length of the message and its height is also perfect but when this cell get adds in UITableView the height of the row is not automatically set to the height of the custom cell so thats why larger text cells get overlapped. Please help me in this regard that how i can set the height of the custom cell.

You can see the error image where cells are overlapped

enter image description here

enter image description here

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
mwaqas01
  • 192
  • 3
  • 11
  • Hope you have implemented - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath – Gaurav Srivastava Dec 09 '15 at 08:26
  • I tried with both options that with implementing this method and return the row height 77 and without implementing this method. Right now i an not using this method and i only specified "Row Height" to 77 from file inspector by selecting my table view. – mwaqas01 Dec 09 '15 at 08:52

2 Answers2

1

You need calculate height for each cell heightForRowAtIndexPath delegate method and return that value like below

- (CGFloat)tableView:(HBTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     CGRect textRect = [<YOUR_MESSAGE> boundingRectWithSize:boundingSize options:NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:<LABEL_FONT>} context:nil];
     return textRect.size.height + <PADDING_LABEL>;
     //PADDING_LABEL = CELL_DEFAULT_HEIGHT-LABEL_DEFAULT_HEIGHT
}
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

Implement this method and calculate row height before returning based on the text length.

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   NSInteger nofLines;
   nofLines = [self getNOFLinesForLabel:296 usingFont:font forText:fullDescription];
   return nofLines*13;
}

-(NSInteger) getNOFLinesForLabel:(CGFloat)width usingFont:(UIFont *)font forText:(NSString *)text {


    CGRect rect = [text boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName : font}
                                     context:nil];

    return ceil(rect.size.height / font.lineHeight);
}

Adjust your label width, font size and other parameters and it will start working porperly.

Vikas Mishra
  • 246
  • 1
  • 12
  • what is "ceil" in "return ceil(rect.size.height / font.lineHeight)" ? – mwaqas01 Dec 09 '15 at 08:58
  • "ceil" and "floor" are Math library functions used to round of float values. ceil returns the next bigger integer e.g. ceil(2.4) will return 3; where as floor just removes the decimal part e.g. floor(2.4) will return 2. – Vikas Mishra Dec 09 '15 at 09:01