iosobjective-cuitableviewlayout

Resize Contents of TableViewCell based on Data pulled from API


I'm trying to get the cell to resize based on the contents that are getting pulled in from an API (image+text).

I have it set right now that I leave space for an Image, a Headline (two lines), and a Description (two lines).

The problem: Sometimes no Image is available, sometimes the Headline is only one line, and sometimes there is no description or 1 line of description; so I need to resize the cell based on these dynamic contents.

WebListCell.m

- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.frame = CGRectMake(1, 20, 320, 180);
    self.headlineLabel.frame = CGRectMake(10, 210, 290, 40);
    self.descriptionLabel.frame = CGRectMake(10, 250, 290, 30);

    [self.headlineLabel setNumberOfLines:2];
    [self.headlineLabel sizeToFit];
    [self.descriptionLabel setNumberOfLines:2];
    [self.descriptionLabel sizeToFit];
}

WebListViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];
    NSString *headlineText = [NSString stringWithFormat:@"%@", feedLocal.headline];
    NSString *descriptionText = [NSString stringWithFormat:@"%@", feedLocal.description];
    cell.headlineLabel.text = headlineText;
    cell.descriptionLabel.text = descriptionText;
    }

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WebListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WebListCell"];
    Feed *feedLocal = [headlinesArray objectAtIndex:indexPath.row];

    NSString *head = [NSString stringWithFormat:@"%@", feedLocal.headline];
    cell.headlineLabel.text = head;
    if (head.length > 100 ){
        [cell.headlineLabel setNumberOfLines:1];
    }
} // WARNING ALERT: "CONTROL REACHES END OF NON-VOID FUNCTION"

I have WebListCell.m that lays out the cell, and then WebListViewController.m that calls WebListCell.m for the layout. I'm pulling in all of the data correctly, but just need help resizing and can't figure it out, even though I've checked out other questions on StackOverflow... can anyone help with this?

Right now what I have doesn't work to resize the cell height.

Much appreciated, and I will post any additional code as needed!


Solution

  • heightForRowAtIndexPath expects you to return a float value.. i.e. the height for the cell. That's the exception being raised.

    To figure out how much vertical space a string occupies, you can use this method:

    CGSize size = [feedLocal.headline sizeWithFont:FONT 
                                          forWidth:WIDTH 
                                     lineBreakMode:NSLineBreakByWordWrapping];
    
    return size.height + SOME_PADDING;
    

    You'll need to experiment with what width and padding looks good. You have some other variables that you mention in the question, but generally, this is what you'll need to do.