Calculate cell height by number of rows?

I would like to create a custom height UITableViewCell using (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.

I am trying to get the height UITableViewCellby multiplying the number of lines in textLabelby the height of each line and then adding 10.0ffor subtitles. I use the following code and get exc_bad_access(). Why?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ([[[tableView cellForRowAtIndexPath:indexPath] textLabel] numberOfLines] * [[[[tableView cellForRowAtIndexPath:indexPath] textLabel] font] lineHeight]) + 10.0;
    //return kRowHeightiPod; //A constant value for a sanity check
}
+3
source share
2 answers

Before you can get a cell, you need to calculate its height. You just calculated its height, depending on the receipt of the cell. You have created infinite recursion.

, , , , , .

, numberOfLines , , , . , , numberOfLines , , .

, NSString UIKit , .

+4

? , , , ...

, - - textLabel. NSZombieEnabled YES , , ...

0

Source: https://habr.com/ru/post/1767964/


All Articles