NSTextField dynimic height in NSTableView - objective-c

There is a NSTextField in a NSTableView.
The height of the text in the window resizing is increased.
but Not grow the cells in a table view.
In this case, the height of table view cell to alter how do you like?
in short, When resizing the size of the text view, I want to change the size of the cell.
like this: (Mac store)
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;
{
KFCustomCellView *cellView = [tableView makeViewWithIdentifier:#"MainCell" owner:self];
NSString *comment = [[_commentList objectAtIndex:row]valueForKey:#"COMMENT"];
[cellView.comment setStringValue:comment];
float cellheight = [comment heightForStringDrawing:comment font:[cellView.comment font] width:self.view.frame.size.width * 0.8];
if (cellheight > cellView.comment.frame.size.height)
{
return (cellheight + 65);
}
return 90;
}

Use following approach the to resize cell height according to textview content :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DataCellId = #"DataCell";
dataCell = (Cell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
if(!dataCell)
{
dataCell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId] autorelease];
}
dataCell.dataCellTextView.tag = indexPath.row;
dataCell.dataCellTextView.text =[yourDataArry objectAtIndex:indexPath.row];
return dataCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define PaddingForTableRowHeight 24
NSString *text;
if([yourDataArry count] > indexPath.row)
{
text = [yourDataArry objectAtIndex:indexPath.row];
} else {
text = #"";
}
UIFont *dataFont = [UIFont boldSystemFontOfSize:16.0f];
CGSize textSize ;
if([UICommonUtils checkIfiOS7])
{
// For iOS7
textSize = [text boundingRectWithSize:CGSizeMake("YOUR CELL WIDTH", MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:dataFont} context:nil].size;
} else {
textSize = [text sizeWithFont:dataFont constrainedToSize:CGSizeMake("YOUR CELL WIDTH" - PaddingForTableRowHeight,MAXFLOAT)];
}
float textViewHeight = MAX(kDefaultCellHeight, textSize.height+PaddingForTableRowHeight);
return textViewHeight;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self scrollToCursorForTextView:textView];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
}
- (void)textViewDidChange:(UITextView *)textView
{
dataUpdated =YES;
if([yourDataArry count] > textView.tag)
{
[yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
}
[self performSelector:#selector(dataReload) withObject:nil afterDelay:0.0];
[self scrollToCursorForTextView:textView];
}
-(void)dataReload
{
[self.tbl beginUpdates];
[self.tbl endUpdates];
}

There are two methods which are used to draw a cell if you know
1-viewForTableColumn
2-objectValueForTableColumn
In the above methods,when you draw your textfield you can give the dynamic height or your row as you are giving in heightforrowatindexpath method.By doing this your cell will get the accurate height. If you have any problem in calculating dynamic height then tell me I'll post the code here for calculating it.

Related

Hide tableview sections on tap gesture of section header

I want to hide the NSArrays (menuItems, about, and charting) on the click for the specific section header for the tableview cell arrays. I got the section header to highlight and de-highlight depending on tap gesture recognizer count but I can not get the tableview cells to hide when that specific section header is clicked. Can someone please help? Thank you! Here is my .m code. My GCF float method is located at the bottom of the .m.
#interface SidebarTableViewController ()
#end
#implementation SidebarTableViewController {
NSArray *menuItems;
NSArray *about;
NSArray *charting;
}
- (void)viewDidLoad {
[super viewDidLoad];
menuItems = #[#"Home",#"Field Goal", #"Punt", #"Kick Off", #"Snapper", #"Punter"];
about = #[#"Home",#"About Us", #"Tutorial"];
charting = #[#"Home",#"Charting"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.section==0) {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
else if(indexPath.section==1) {
NSString *CellIdentifier2 = [charting objectAtIndex:indexPath.row];
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
return cell2;
}
else {
NSString *CellIdentifier1 = [about objectAtIndex:indexPath.row];
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
return cell1;
}
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0)
{
return [menuItems count];
}
else if(section==1)
{
return [charting count];
}
else{
return [about count];
}
}
- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor grayColor];
if(section == 0){
headerLabel.text = [NSString stringWithFormat:#"Timers Without Charting"];
}
else if(section==1)
{
headerLabel.text = [NSString stringWithFormat:#"Charting with Timers"];
}
else{
headerLabel.text = [NSString stringWithFormat:#"About Us/Tutorial"];
}
headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(catchHeaderGesture:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
//return nil;
}
-(void)catchHeaderGesture:(UIGestureRecognizer*)sender
{
border ++;
if (border == 1)
{
UILabel *caughtLabel = (UILabel*)sender.view;
caughtLabel.layer.borderColor = [UIColor yellowColor].CGColor;
caughtLabel.layer.borderWidth = 2;
}
if (border == 2 )
{
UILabel *caughtLabel = (UILabel*)sender.view;
caughtLabel.layer.borderColor = [UIColor clearColor].CGColor;
caughtLabel.layer.borderWidth = 2;
border = 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
border ++;
if (border == 1)
{
UITableViewCell* cell = [menuItems objectAtIndex:indexPath.row];
cell.hidden = YES;
return 40.0;
}
if (border == 2 )
{ border = 0;
UITableViewCell* cell = [menuItems objectAtIndex:indexPath.row];
cell.hidden = YES;
}
return 0;
}
#end
You need a few couple things to make this work.
section headers that respond to taps.
a method for expanding or collapsing a section.
some way to track which sections are collapsed.
The first is trivial. Return a UIView for the header, and attach a UITapGestureRecognizer to it. You'll need a method to figure out which section it is. You can use the tag property, or you can store the views in an NSMutableArray.
In tableView:numberOfRowsInSection: you return 0 if the section is collapsed, or the actual number, if not.
In the handler for the gesture recognizer, you toggle the collapsed/expanded state, and then you call `[self.tableView reloadSections:withRowAnimation:] to update the visuals.
(I do see in your posted code that you already handle part of this.)

How to calculate number lines required for text

I know this is going to be a stupid daft simple question but I'm going round in circles.
I have several strings which i want displaying in a uitableview. Some of these strings are very long. I have previously asked how to calculate the cell height, and choose the following answer:
- (CGFloat)cellHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellString = <YOUR MODEL OBJECT>;
NSDictionary *attributes = #{ NSFontAttributeName : [UIFont systemFontOfSize:16.0f] };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithString: cellString
attributes:attributes];
CGFloat width = self.tableView.frame.size.width - 32.0f;
CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
// Add some extra padding to the height
CGFloat height = frame.size.height + 16.0f;
return ceil(height);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self cellHeightForRowAtIndexPath:indexPath];
}
how can I get the number of lines required in the uitableview to display the strings.
The best way to actually calculate a cell height is to use a prototype cell for calculating the height.
Add a property to your interface extension:
#interface TableViewController ()
#property (nonatomic, strong) TableViewCell *prototypeCell;
#end
Then lazily load it:
- (TableViewCell *)prototypeCell
{
if (!_prototypeCell)
{
_prototypeCell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Prototype"];
}
return _prototypeCell;
}
Change your -[tableView:cellForRowAtIndexPath:] method to use a -[configureCell:forRowAtIndexPath:] type pattern:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
-(void)configureCell:(TableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set up you cell from your model
}
Now use this method to setup your prototype and then use the height of that for -[tableView:heightForRowAtIndexPath:]:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self configureCell:self.prototypeCell forRowAtIndexPath:indexPath];
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return ceil(size.height);
}

Resizing UITextView and UITableViewCell

I have new to objective C programming and i am currently trying to develop an iOS app. I am loading comment from the server onto the UITextView in the UITableViewCell.
This is the code that is used to load data into the UITextview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// set the location to read the sample data
static NSString *CellIdentifier = #"CommentCell";
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PostModel *posts;
CommentModel *comments;
if (contentTitle == nil) {
posts = _feed.posts[articleIndexPath.section];
comments = posts.comments[indexPath.section];
}
else {
posts = _feed.posts[searchResult];
comments = posts.comments[indexPath.section];
}
NSString *content = comments.content;
content = [content stringByReplacingOccurrencesOfString:#"<p>" withString:#""];
content = [content stringByReplacingOccurrencesOfString:#"</p>" withString:#""];
cell.usernameLabel.text = comments.name;
[cell.commentTextView setScrollEnabled:YES];
cell.commentTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.commentTextView.text = content;
[cell.commentTextView sizeToFit];
[cell.commentTextView setScrollEnabled:NO];
[cell.commentTextView setTextColor:[UIColor whiteColor]];
[cell.usernameLabel setTextColor:[UIColor whiteColor]];
// make the borders of the cell round
[cell.layer setCornerRadius:7.0f];
[cell.layer setMasksToBounds:YES];
cell.contentView.layer.borderColor = [[UIColor colorWithRed:51/255 green:56/255 blue:67/255 alpha:1] CGColor];
//make the borders of the image round
[cell.userImage.layer setMasksToBounds:YES];
[cell.userImage.layer setCornerRadius:7.0f];
return cell;
}
This is the code that i used to resize the UITableViewCell and UITextView
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CommentCell";
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PostModel *posts;
CommentModel *comments;
if (contentTitle == nil) {
posts = _feed.posts[articleIndexPath.section];
comments = posts.comments[indexPath.section];
}
else {
posts = _feed.posts[searchResult];
comments = posts.comments[indexPath.section];
};
if (cell.commentTextView.textContainer.size.height >= 40) {
float height = [self heightForTextView:cell.commentTextView containingString:comments.content];
return height;
}
else {
return 79;
}
}
This is the method that i used to detect the height of the text
- (CGFloat)heightForTextView:(UITextView *)textView containingString:(NSString *)string {
float horizontalPadding = 8;
float verticalPadding = 16;
float widthOfTextView = textView.textContainer.size.width - horizontalPadding;
float height = [string sizeWithFont:textView.font constrainedToSize:CGSizeMake(widthOfTextView, 999999.0f) lineBreakMode:NSLineBreakByWordWrapping].height + verticalPadding;
return height;
}
- (CGSize)text: (NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7")) {
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
CGRect frame = [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];
return frame.size;
}
else {
return [text sizeWithFont:font constrainedToSize:size];
}
}
It load fine when i run it initially but when i scroll down and back up, the UITextView became a vertical single line column.
Any help would be greatly appreciated!
I think the this might be the cause:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
You are using a dequeued cell's text view to determine the height of the cell. However the dequeued cell may not having any text data (or different text) in it, plus the commentView is UIViewAutoresizingFlexibleWidth, the width calculated by
float widthOfTextView = textView.textContainer.size.width - horizontalPadding;
may be very small under the scenario of scroll down and backup.

Changing height dynamic for Table Rows , CustomCell and Layout

There is a custom cell. In it there is two labels. One of them called nameLabel's height has to change dynamically. It can be 1,2 or 3 lines sometimes. But the rows are on eachother, they cross their own row lines. How can I solve this problem?
The labels and Custom Cell object's Use Autolayout option is disabled.
The label height has to change dynamic, And then the CustomCell's. And then tableRow. My head is confused. And why can't I see CustomCell's background color is changing?
Thanks
Here is my code for :
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = [[self.dataList objectAtIndex:indexPath.row] objectForKey:#"NAME"];
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(320, 63) lineBreakMode:NSLineBreakByWordWrapping];
CGFloat h = labelSize.height;
NSInteger x=0.0;
if (h==63.0) x=30;
if (h==42.0) x=20;
if (h==21.0) x=10;
return h+30;
}
- (UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"cellid";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = (CustomCell*)[[[NSBundle mainBundle]
loadNibNamed:#"CustomCell" owner:nil options:nil]
lastObject];
}
// customization
NSDictionary *d = [self.dataList objectAtIndex:indexPath.row];
cell.nameLabel.text = [d objectForKey:#"NAME"];
cell.cityLabel.text = [d objectForKey:#"CODE"];
cell.indexPath = indexPath;
return cell;
}
At this link there is picture of simulator and xib of the Custom Cell to understand easy the problem:
http://compfreek.wordpress.com/2013/08/14/custom-cell-for-table-row-height-changes-dynamic/
Hear is another way try this change according to ur code it may different, but it may solve your problem.I am putting all views through code only, because it is easy for me check this out.
//in your subclassed class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//may be u did same i am adding the label to cell
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
nameLabel.numberOfLines = 5; //set number of lines
nameLabel.tag = 100;
[self addSubview:nameLabel];
UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectZero];
otherLabel.tag = 200;
[self addSubview:otherLabel];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
//do your customisation when cell is selected
}
-(void)layoutSubviews
{
//in this method i am setting the frames for label
[super layoutSubviews];
UILabel *nameLabel = (UILabel *) [self viewWithTag:100];
nameLabel.text = self.nameString;
CGSize maxSize = CGSizeMake(self.bounds.size.width / 2, MAXFLOAT);//set max height
CGSize nameSize = [self.nameString sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
nameLabel.frame = CGRectMake(self.bounds.origin.x +2,self.bounds.origin.y+3, nameSize.width, nameSize.height);
UILabel *otherLabel = (UILabel *) [self viewWithTag:200];
otherLabel.frame = CGRectMake(nameLabel.frame.size.width+15,self.bounds.origin.y+3, 100, 40);
otherLabel.text = self.otherString;
}
//in your main class
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if(cell == nil)
{
cell = [[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"]autorelease];
}
//change it for ur need
NSString *name = #"hear is your long length name uzamaki naruto from uzamaki clan";
NSString *other = #"other name";
cell.nameString = name;
cell.otherString = other;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *name = #"hear is your long length name uzamaki naruto from uzamaki clan";
//replace it your height logic
float cellWidth = 350; // ur cell width
CGSize maxSize = CGSizeMake( cellWidth / 2, MAXFLOAT);//set max height
CGSize nameSize = [name sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];
return nameSize.height + 10;// for ur height
}

Changing the size of a UITableViewCell based on the size of a subview

How can I scale UITableViewCells based on the amount of content in them? In my cells I use three labels which represent a forum. The labels are named "alias", "date", and "comments". The third label, comments, can be any number of rows. Therefore, I need my cells to become dynamically size, depending on the amount of text in the "comments" label. Here is my code:
- (BOOL)textFieldShouldReturn:(UITextField *)pTextField
{
[self setLoadingState:YES];
[pTextField resignFirstResponder];
NSUserDefaults *userStorage = [NSUserDefaults standardUserDefaults];
NSString *alias = [self urlEncode:[userStorage objectForKey:#"alias"]];
NSString *email = [self urlEncode:[userStorage objectForKey:#"email"]];
NSString *who = [self getUniqueDeviceId];
NSString *comment = [self urlEncode:[pTextField text]];
comment = [comment stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
who = [who stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([self isBlank:comment])
{
[self setLoadingState:NO];
pTextField.text = #"";
return NO;
}
if([self isBlank:alias])
{
[self showMessagePopup:NSLocalizedString(#"MessageMustChooseAlias", nil)];
return NO;
}
[self.forumThreadDataProvider startSendPost:self.taskId : self.forumThreadId : alias : who : email : comment];
pTextField.text = #"";
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Feedback *item = [self.items objectAtIndex:indexPath.row];
UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];
[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];
commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];
return cell;
}
Ive tryed already myself with the following code-example but it failed big-TIME:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell) {
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
return commentLabel.frame.size.height;
}
else
return 30;
}
Check this tutorial for how to set dynamically set cell height,
Basically you need to use methods like this to calculate the height of Laebl,
- (CGFloat)RAD_textHeightForSystemFontOfSize:(CGFloat)size {
//Calculate the expected size based on the font and linebreak mode of the label
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat maxHeight = 9999;
CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);
CGSize expectedLabelSize = [self sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}
Then you need to implement the heightForRowAtIndexPath method,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *label = [self.aNote length] == 0 ? kDefaultNoteLabel : self.aNote;
CGFloat height = [label RAD_textHeightForSystemFontOfSize:kTextViewFontSize] + 20.0;
return height;
}