2
votes

I have a UITableViewController that displays a custom UITableViewCell (inherited from UITableViewCell). That same UITableViewCell has a UILabel that can have text of variable length. The challenge I am having is how to access this UILabel in my UITableViewController so that I can set the correct cell height in heightForRowAtIndexPath.

Or on a side note how do I solve my problem of having a dynamicically sized label.

thanks

Here is my code for the custom UITableViewCell:

header:

#import <UIKit/UIKit.h>

@interface MessagesCustomViewCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *message;   <---- need to access this 

@end

implementation:

#import "MessagesCustomViewCell.h"

@implementation MessagesCustomViewCell
@synthesize message=_message;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
2
Could you post the solution ? I refere to the + (CGFloat)heightForMessage:(NSString *)message; implementation.doxsi

2 Answers

3
votes

Using the way of measuring the font size mentioned by Ivan, you could additionally add a class metod to MessagesCustomViewCell,

+ (CGFloat)heightForMessage:(NSString *)message;

in which you calculate the height of message using the appropriate UILabel width/height, font etc. This could be called from heightForRowAtIndexPath: as such:

NSString *dynamicString = [self.mydata objectAtIndex:indexPath.row];
CGFloat height = [MessagesCustomViewCell heightForMessage:dynamicString];
return height;
1
votes

Call [tableView reloadData] once data is updated.

Check the size with [yourString sizeWithFont:(UIFont*) forWidth(CGFloat) lineBreakMode:(NSLineBreakMode)] method in heightForRowAtIndex. The referred method will return a required size (including the height).