Question

I read a lot of tutorials about it but still can't make it right. Here is the code:

@implementation DCHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.tableViewContentArray = @[@"The franchise centers around a series of fantasy and science fantasy role-playing video games (RPGs), but includes motion pictures, anime, printed media, and other merchandise. The eponymous first game in the series, published in 1987",
                                   @"racing, third-person shooter, fighting, and rhythm.",
                                   @"e franchise. Recurring elements include plot themes, character names, and game mechanics. Plots center on a group of heroes battling a great evil while exploring the characters' internal struggles and relationships. Character names are frequently derived from the history, languages, and mythologies of cultures worldwide.",
                                   @"l Fantasy has been a driving force in the video game industry, and the series has affected Square Enix's business practices and its relationships with other video game developers. It has also introduced many features now common in role-playing video games and has been credited with helping to popularize console-based RPGs in markets outside Japan.",
                                   @" in Japan, has been bundled with Final Fantasy in several re-releases.[3][4][5] The last of the NES installments, Final Fantasy III, was release",
                                   @"he first six games to three-dimensional (3D) computer graphics; the game features polygonal characters on pre-rendered backgrounds. It also introduced a more modern setting, a style that was carried over to the next game.[3] It was also the first in the series to be released in Europe. The eighth installment was published in 1999, and was the first to consistently use realistically proportioned characters and feature a vocal piece as its theme music.[3][13] Final Fantasy IX, released in 2000, returned to the series' roots by revisiting a more traditional Final "];
}

#pragma mark - Table View methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }

    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    [cell.textLabel setLineBreakMode:NSLineBreakByCharWrapping];
    cell.textLabel.text = self.tableViewContentArray[indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont systemFontOfSize:14.0f];
    gettingSizeLabel.numberOfLines = 0;
    gettingSizeLabel.text = self.tableViewContentArray[indexPath.row];
    [gettingSizeLabel setLineBreakMode:NSLineBreakByCharWrapping];
    CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, CGFLOAT_MAX);

    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];


    CGFloat height = expectedSize.height;

    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableViewContentArray count];
}

@end

So as the result I have incorrect height, and cell's content (if it's too long) cropped. Why does it happen?

UPD: also I've tried to use boundingRectWithSize method of NSString, with exactly the same attributes:

NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],NSStrokeColorAttributeName: [UIColor grayColor]}. 

It does not work at all - it makes only 2 lines of text and very big width. Looks like it is just ignoring

CGSize templateSize = CGSizeMake(300.0f, CGFLOAT_MAX) that I have set.
Was it helpful?

Solution

Was fixed with this:

CGSize maximumLabelSize = CGSizeMake(290.0f, CGFLOAT_MAX);

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

CGFloat height = expectedSize.height + 1;

290 because of cell.textLabel's width. But! height+1 was some kind of improvisation. So I still does no know why should I make this "+1", but it works. Does anybody know what is this 1 means and why is not it included in sizeThatFits returned value?

OTHER TIPS

try this...

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        float hh=20;

        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, FLT_MAX);
            CGSize expectedLabelSize = [[self.tableViewContentArray objectAtIndex:indexPath.row] sizeWithFont: [UIFont systemFontOfSize:14.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByCharWrapping];
            NSLog(@"%f",expectedLabelSize.height);
            hh+=expectedLabelSize.height;
        } else {
            // code here for iOS 7.0

            CGSize constrainedSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, FLT_MAX);

            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont systemFontOfSize:14.0f], NSFontAttributeName,
                                              nil];

            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[self.tableViewContentArray objectAtIndex:indexPath.row] attributes:attributesDictionary];

            CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
            NSLog(@"%f",requiredHeight.size.height);
            hh+=requiredHeight.size.height;

        }

        return hh;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top