0

I'd like to know if this is possible:

  • when viewing a table containing multiple comments from a post, each comment may have different lengths - so the table cells should resize vertically to accomodate more text.

Note I am looking for a different solution than posted here and elsewhere in SO, because I'd like to achieve this result without having to add code to my controller.

Using IB, my cell uses:

  • style: subtitle
  • mode: scale to fit
  • row height: default

My "Title" label (which is the one that should expand):

  • line breaks: word wrap
  • lines: 0

With the above, I actually get to see the multiple lines of text, but the rows do not resize accordingly -- so the text from several rows gets overlapped.

Is it possible to have the rows resize vertically without coding this into my controller?

CommentViewController.m

#import "CommentViewController.h"

@implementation CommentViewController
@synthesize     commentsArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return commentsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"commentCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *comment       = [commentsArray objectAtIndex:indexPath.row];
    NSString     *commentText   = [comment objectForKey:@"comment_text"];
    NSString     *commentAuthor = [comment objectForKey:@"comment_author_name"];

    cell.textLabel.text       = commentText;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", commentAuthor];

    return cell;
}

@end
Community
  • 1
  • 1
pepe
  • 9,145
  • 24
  • 100
  • 177
  • 1
    I'm 99% certain (I could always be wrong, of course) that this isn't possible using Interface Builder only- especially if your labels are different heights. Is there a particular reason why you're trying to avoid the use of code? – username tbd Nov 29 '12 at 04:15
  • just for simplicity sake... and because I'm not being able to implement the code from the linked answer I referred to... I've tried to merge that code with the above controller and I think I'm screwing up big time – pepe Nov 29 '12 at 04:18
  • What exactly was the problem? We can probably help fix it. – username tbd Nov 29 '12 at 04:20
  • I'm unsure which parts of the code should go where - for example, in the above I send `textLabel` and `detailTextLabel` via a `return cell`. But where would that go in the code from the other answer? – pepe Nov 29 '12 at 04:32
  • nevermind @username tbd - I managed to fix it - will post answer in a sec - thx for chiming in! – pepe Nov 29 '12 at 04:36

1 Answers1

0

Here's how I merged the code from the SO answer I mentioned with my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"commentCell";

    NSDictionary *comment       = [commentsArray objectAtIndex:indexPath.row];
    NSString     *commentText   = [comment objectForKey:@"comment_text"];
    NSString     *commentAuthor = [comment objectForKey:@"comment_author_name"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
    }

    cell.textLabel.text       = commentText;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", commentAuthor];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *comment       = [commentsArray objectAtIndex:indexPath.row];
    NSString     *commentText   = [comment objectForKey:@"comment_text"];

    UIFont *cellFont      = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize      = [commentText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

    return labelSize.height + 40;
}

@end
pepe
  • 9,145
  • 24
  • 100
  • 177