Dynamic Height For UITableViewCell and The content inside - iphone

I have a particularly problem that i can't seem to solve i have looked at my code for a few hours and can't seem to fix the problem.
When i add messages to my tableview the messages overflows the wrapper within the UITableViewCell and sometimes the wrapper gets cutoff by the cell.
How do i make sure that my wrapper is always big enough to hold the txt (lblMessage)
and the cell is big enough to hold the wrapper.
Thanks in advance !
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [self heightForMessage:indexPath];
if(height < 50)
{
NSLog(#"Normal : %d , row : %d", 100,indexPath.row);
return 100;
}
else
{
NSLog(#"Custom : %f , row : %d", 70 + height,indexPath.row);
return 70 + height;
}
}
-(CGFloat)heightForMessage:(NSIndexPath *)indexPath
{
MessageObject * model = [self.chats objectAtIndex:indexPath.row];
CGSize size = [model.message sizeWithFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:12] constrainedToSize:CGSizeMake(290, 100000) lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * NewsCellIdentifer = #"NewsCellIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];
UILabel * lblContact;
UILabel * lblMessage;
UILabel * lblDate;
UIView * wrapper;
if (cell == nil)
{
/* Top menu for login */
wrapper = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 90)];
[wrapper setBackgroundColor:[self.delegate.color colorWithAlphaComponent:0.6f]];
[wrapper.layer setCornerRadius:6.0f];
[wrapper.layer setShadowOffset:CGSizeMake(0, 2)];
[wrapper.layer setShadowRadius:2.0f];
[wrapper.layer setShadowOpacity:0.5f];
[wrapper viewWithTag:19];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NewsCellIdentifer];
/* Contact Name */
lblContact = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, 30)];
[lblContact setFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:13]];
[lblContact setTag:13];
[lblContact setTextColor:[self.delegate colorFromHexString:#"#fffbff"]];
[lblContact setBackgroundColor:[UIColor clearColor]];
/* Received Message */
lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 50)];
[lblMessage setNumberOfLines:0];
[lblMessage setLineBreakMode:NSLineBreakByWordWrapping];
[lblMessage setBackgroundColor:[UIColor clearColor]];
[lblMessage setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:12]];
[lblMessage setTextColor:[self.delegate colorFromHexString:#"#fffbff"]];
[lblMessage setTag:14];
/* Date received */
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, 65, 30)];
[lblDate setText:#"4 hours ago"];
[lblDate setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:11]];
[lblDate setTextColor:[self.delegate colorFromHexString:#"#fffbff"]];
[lblDate setTag:15];
/* Subview Logic */
[wrapper addSubview:lblContact];
[wrapper addSubview:lblMessage];
[wrapper addSubview:lblDate];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView addSubview:wrapper];
}
else
{
lblContact = (UILabel *)[cell.contentView viewWithTag:13];
lblMessage = (UILabel *)[cell.contentView viewWithTag:14];
wrapper = [cell.contentView viewWithTag:19];
}
MessageObject * model = [self.chats objectAtIndex:indexPath.row];
CGFloat height = [self heightForMessage:indexPath];
[lblMessage setFrame:CGRectMake(10, 25, 280, height)];
[lblMessage setText:model.message];
[lblContact setText:model.clientModel.firstName];
if(height < 50)
{
NSLog(#"wrapper size : %d for row %d ", 90, indexPath.row);
[wrapper setFrame:CGRectMake(10, 5, 300, 90)];
}
else
{
NSLog(#"wrapper size : %f for row %d ", height + 60, indexPath.row);
[wrapper setFrame:CGRectMake(10, 5, 300, height + 60)];
[lblDate setFrame:CGRectMake(10, height + 20, 65, 30)];
}
[cell.contentView setBackgroundColor:[UIColor greenColor]];
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}

Consider significantly improve your code...Cause it's almost unreadable now
1) Get rid of hardcode e.g. 20, 280. Instead consider using constants (CONTACT_LABEL_TO_MESSAGE_LABEL_OFFSET, MESSAGE_LABEL_WIDTH)
2) Get rid of adding custom subviews to UITableViewCell and tagging them with hardcoded (see 1) numbers - instead subclass UITableViewCell, Prototype it in .xib or .storyboard, make convinient methods for your class - e.g setDate:, setMessage:, setContactName:. All frames should be calculated in your subclass -layoutSubviews method.
At first look:
1) you set width of 280 to lblMessage, when you constrained it's text to width of 290, it causes wrong height calculation

One of the errors is when you are creating the wrapper view for a new cell the line:
[wrapper viewWithTag:19];
should be:
[wrapper setTag:19];
This probably causes wrapper to be nil when you attempt to resize it on a reused cell.

I recommend using a prototype cell and computing cell heights by literally configuring the prototype with the data for the given index path. Just dequeue a cell (yes, you can do this outside of cellForRowAtIndexPath) and hold onto it for doing height calculations. For dynamic height labels, you'd typically call [label sizeToFit] in the cell's configuration logic. There is a more detailed discussion of this technique in this question.
If you want to take a look at a 3rd-party library, my TLIndexPathTools library provides a base table view class TLTableViewController with built-in support for dynamic row height using prototypes:
For static, non-data driven height, it will return the default height of the prototype. So, if your prototype comes from the Storyboard, it will honor the Storyboard custom cell heights.
For dynamic, data-driven height, any custom cell that implements the TLDynamicHeightView protocol will automatically have the dynamic height calculated.

I'll tell you the approach i used to tackle this issue.
Used an array to store heights (using sizeWithFont). It is deprecated now, so you should try using sizeWithAttributes instead.
Log the height from the array, and the height of the actual label, and see if they match.
Make sure the UIFont used is the same in sizeWithFont, and yourLabel.font.
Check to see if you have taken any kind of padding in the cells, like leaving pixels on the top of the cell.
These are the general tips.

When you create a cell in tableView: cellForRowAtIndexPath: you can store it into a NSMutableArray
Then when tableView: heightForRowAtIndexPath: simply retrieve the cell and specify it height.
It's how I do it most of the time but if you use a lot of cell it might use to much memory.
Or maybe you can only store the size of the cell in the NSMutableArray.
EDIT:
I'm not sure I understand you correctly.
You want your cell to be able to contain you wrapper?
cell.contentView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, wrapper.frame.origin.y + wrapper.frame.size.height);
Store the cell in an NSMutableArray in tableView: cellForRowAtIndexPath:, retrieve it based on it index in tableView: heightForRowAtIndexPath:

To calculate your Label height you should consider different things, You have to calculate size by keeping text length and its font in mind and the bounds of frame you are adding in. Below is example how to calculate size for dynamic text.
CGSize boundingSize = CGSizeMake(frame.size.width, CGFLOAT_MAX);
CGSize requiredSize = [text sizeWithFont:yourFont constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping];

Related

UILabel in a grouped table view's header doesn't wrap text

I have a label whose text is longer that the table view's header containing it, so I want the label to be split into N lines according to the width available. This is my code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
UIView *wrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
[wrapper setBackgroundColor:[UIColor clearColor]];
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
textLabel.text = NSLocalizedString(#"This is supposed to be a very long text that may fill several lines", #"");
[textLabel setLineBreakMode:UILineBreakModeWordWrap];
[textLabel setNumberOfLines:0];
[wrapper addSubview:textLabel];
return wrapper;
}
else
return nil;
}
However, the label is in a single line and I can't see the end of the text. What am I missing?
Thanks!
You should implement the delegate method heightForHeaderInSection.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section ==1)
return 100;
else
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
NSString *text = NSLocalizedString(#"This is supposed to be a very long text that may fill several lines", #"");;
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:10] constrainedToSize:CGSizeMake(self.tableView.frame.size.width, 999) lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = CGRectZero;
frame.size = size;
UIView *wrapper = [[UIView alloc] initWithFrame:frame];
[wrapper setBackgroundColor:[UIColor clearColor]];
UILabel *textLabel = [[UILabel alloc] initWithFrame:frame];
textLabel.text = NSLocalizedString(#"This is supposed to be a very long text that may fill several lines", #"");
[textLabel setLineBreakMode:UILineBreakModeWordWrap];
[textLabel setNumberOfLines:0];
[wrapper addSubview:textLabel];
return wrapper;
}
else
return nil;
}
use this if you want to support iOS below 6.0
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
As UILineBreakModeWordWrap deprecated now.
otherwise use this
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
you should set size of label as text in heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *text;
CGSize textViewSize;
int width = 270;
textViewSize = [text sizeWithFont:[UIFont fontWithName:#"Helvetica Neue" size:14.0]
constrainedToSize:CGSizeMake(width, FLT_MAX)
lineBreakMode:UILineBreakModeCharacterWrap];
CGFloat height = MAX(textViewSize.height, 44.0f);
return height;
}
and in cellForRowAtIndexPath write this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
textViewSize = [text sizeWithFont:[UIFont fontWithName:#"Helvetica Neue" size:14.0]
constrainedToSize:CGSizeMake(width, FLT_MAX)
lineBreakMode:UILineBreakModeCharacterWrap];
// / CGFloat height = MAX(textViewSize.height, 44.0f);
// if (!lblParameter){
// lblParameter = (UILabel*)[cell viewWithTag:1];}
CGFloat height = MAX(textViewSize.height, 44.0f);
[lblParameter setText:text];
[lblParameter setFont:[UIFont fontWithName:#"Helvetica Neue" size:14.0]];
[lblParameter setFrame:CGRectMake(10, 0,width,height)];
[[cell contentView] addSubview:lblParameter];
}
you need caculate the height of uilabel this height equal font size * numberline
It seems that wrapping / formatting will not work on a label inside the header view - presumably because it's inheriting defaults from somewhere. So, a simple solution, which AFAIK breaks no laws, is to put the label inside a view inside the header view.
Using the storyboard, drag a view to the table view controller (above your prototype cell).
Add a second view to this view.
Add the label to this second view. Make sure both views and label are tall enough to allow for multiple lines of text. In the label's attributes, set Lines to 0 and Line Breaks to Word Wrap.
Wrapping will now work as expected.

iPhone When increasing the height of UITableView section header the height of the cell below it decreases. How to avoid?

I created a UITableView of which I adjusted the height and background of the section header. This all works as expected but the cell (row) below it becomes smaller. It almost seems that the section header goes over the first cell.
Is there a way to solve this? Or should I ask, how to solve this?
My code of the section header:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 30)];
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
[headerLabel setTextColor:[UIColor whiteColor]];
headerLabel.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:#"menuSectionBg.png"]];
[headerView addSubview:headerLabel];
return headerView;
}
You should not adjust the height of the cells' views, but instead implement the
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
method in your UITableViewDelegate implementation.
The heights for the cells are calculated before the cells are even created.

UITableView Section Header Font Issue

I'm trying to customize the section header for a UITableView. The UITableView was created in IB. I'm having 2 issues that I cannot figure out.
The font size will not increase past about 18 or something regardless of how large the UILabel is.
The section headers are obscuring the tables.
I've colored the labels blue so you can see their size.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Name of section
NSString *header = [[purchases allKeys] objectAtIndex:section];
// Label for section header
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(10, 0, 230, 45);
label.textColor = _orange;
label.font = [UIFont fontWithName:#"Heiti TC Medium" size:52];
label.text = header;
label.backgroundColor = [UIColor blueColor];
// View to contain label
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 230, 45)];
[view autorelease];
[view addSubview:label];
return view;
}
You should add table view delegate method for header height
You should use all cell width for header view.
Your font named as STHeitiTC-Medium

dynamic calculation of UILabel width in UITableViewCell

I am building a custom, in-application settings view based on UITableViewCellStyle1 (or so).
I am trying to dynamically calculate the width of the left label (title) of a cell to determine how wide my text field on the right can be.
When I launch the app in the simulator and the view loads, the width of the title labels is zero until I scroll down the view and a cell is not visible, then when I scroll back up the size adjusts as expected. I believe this might have to do something with cell reusage.
I need the title labels in the cells to have their correct width when the view loads, not after they have gone out of sight one time.
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* reuseIdentifier = #"SettingsCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if ( !cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
[cell setBackgroundColor:[UIColor baeDarkGrayColor]];
UILabel* cellLabel = [cell textLabel];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cell setUserInteractionEnabled:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
// Populate cell with corresponding data.
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section];
// Set the label
UILabel* textLabel = [cell textLabel];
NSString* labelString = [[tableSection objectForKey:#"itemTitles"] objectAtIndex:indexPath.row];
[textLabel setText:labelString];
// CGSize constrainedSize;
// constrainedSize.width = MAXFLOAT;
// constrainedSize.height = MAXFLOAT;
CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font /*constrainedToSize:constrainedSize*/];
NSLog(#"text label width: %f", textLabelSize.width);
// Set the accessory view
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:#"itemAccessories"] objectAtIndex:indexPath.row] integerValue];
switch ( accessoryType )
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"AccessoryDisclosure.png"]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
case BAESettingsTableCellAccessoryNone:
// default:
// break;
{
CGRect cellFrame = [cell frame];
float detailTextFieldWidth = cellFrame.size.width - ( textLabelSize.width + 50 );
float detailTextFieldHeight = cellFrame.size.height - 1;
NSLog(#"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight);
CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight);
UITextField* textField = [[UITextField alloc] initWithFrame:frame];
NSString* detailLabelString = [[tableSection objectForKey:#"itemDetailStrings"] objectAtIndex:indexPath.row];
textField.text = detailLabelString;
textField.textColor = cell.detailTextLabel.textColor;
textField.textAlignment = UITextAlignmentRight;
textField.borderStyle = UITextBorderStyleRoundedRect;
[cell setAccessoryView:textField];
[textField release];
break;
}
}
return cell;
}
Explicitly setting the font solves your problem.
CGSize textLabelSize = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];
Gory Detals:
When the tableview first loads, this snippet returns zero width due to the font size being 0.
[textLabel.text sizeWithFont:textLabel.font]
I figured this out by displaying the point size of the cell font.
NSLog(#"font size: %f", cell.textLabel.font.pointSize);
The point size was zero until the cell went off screen and came back, just as you described.
I completely agree that it's sufficiently annoying that it should be a bug. Why would the nice folks at Apple ever ask for the cell height without ensuring the cell is completely set up. Silly! However, instead of using a deprecated function, try:
[cell layoutIfNeeded];
Calling this method has the same effect with the cell being set up including the bounds, cell font, etc. Therefore, any calculation after this has access to the fully set up cell. Here's the whole code:
-(CGFloat) tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath {
// Find the cell for this index path
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
CGFloat cellHeight = cell.frame.size.height;
// Calculate text size after forcing a layout
[cell layoutIfNeeded];
CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
// Only change the cell height if the text forces a growth
if (textSize.height > cellHeight) {
cellHeight = textSize.height;
}
return cellHeight;
}
I found the same issue, but I've found a workaround. FYI I've raised an bug with Apple (Bug ID# 7535066). Lex, in your example, if you use cell.font instead of textLabel.font, it should work, although you will get a compiler warning, as this is a deprecated method call. I'm not sure if you'll be able to see the details of the bug I raised, so I'm pasting the details here:
iPhone SDK 3.1.2 (7D11) NSString UIKit Additions method sizeWithFont returns nil
SUMMARY
I am decorating UITableViewCells in the UITableView delegate method:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I wish to find length of the text already in the cell, to correctly format the new view, so I am calling the NSString UIKit Additions method:
(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
When I use cell.textLabel.font for the font argument, it always returns an empty CGSize
IF I use the DEPRECATED method, cell.font for the argument, it DOES return the CGSize value as expected.
STEPS TO REPRODUCE
1. Create a UITableViewController with a TableView, and populate it with some sample data
2. In the stubbed method cellForRowAtIndexPath, after the comment //Set up the cell, and where the cell.textLabel.text is populated, put the following:
CGSize size = [cell.textLabel.text sizeWithFont:[[cell textLabel] font] constrainedToSize:CGSizeMake(320, 480)];
NSLog(#"cell frame: %f %f", size.width, size.height);
CGSize size2 = [cell.textLabel.text sizeWithFont:[cell font] constrainedToSize:CGSizeMake(320, 480)];
NSLog(#"DEPRECATED cell frame: %f %f", size2.width, size2.height);
EXPECTED RESULTS
If anything, you would expect the call that uses the Deprecated method to retrieve the font, to fail, or at least both to return the same result.
ACTUAL RESULT:
2010-01-12 22:06:36.645 PrefTest[9659:207] cell frame: 0.000000 0.000000
2010-01-12 22:06:36.646 PrefTest[9659:207] DEPRECATED cell frame: 88.000000 24.000000
WORKAROUND & ISOLATION:
As noted, use the deprecated method to get the font. But it gets stranger!
Reverse the two methods I gave above:
CGSize size2 = [cell.textLabel.text sizeWithFont:[cell font] constrainedToSize:CGSizeMake(320, 480)];
NSLog(#"DEPRECATED cell frame: %f %f", size2.width, size2.height);
CGSize size = [cell.textLabel.text sizeWithFont:[[cell textLabel] font] constrainedToSize:CGSizeMake(320, 480)];
NSLog(#"cell frame: %f %f", size.width, size.height);
The results are now:
2010-01-12 22:06:36.645 PrefTest[9659:207] cell frame: 88.000000 24.000000
2010-01-12 22:06:36.646 PrefTest[9659:207] DEPRECATED cell frame: 88.000000 24.000000
It seems that just referencing [cell font] is enough to force [[cell textLabel] font] to work correctly.
Use [myLbl sizeToFit]; to automatically resize the label.
Here is the sample code.
UILabel *lbl1, *lbl2;
if (cell == nil) {
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl1];
lbl1.frame = CGRectMake(3, 10, 0, 0);
lbl1.tag = 10;
[lbl1 release];
UILabel *lbl2 = [[UILabel alloc] init];
[lbl2 setFont:[UIFont systemFontOfSize:20]];
[cell.contentView addSubview:lbl2];
lbl2.tag = 20;
[lbl2 release];
}
else {
lbl1 = (UILabel*)[cell.contentView viewWithTag:10];
lbl2 = (UILabel*)[cell.contentView viewWithTag:20];
}
lbl1.text = #"Hello ";
[lbl1 sizeToFit];
lbl2.text = #"World";
[lbl2 sizeToFit];
lbl2.frame = CGRectMake(lbl1.frame.origin.x+lbl1.frame.size.width+5,
lbl1.frame.origin.y,
lbl2.frame.size.width,
lbl1.frame.size.height);

Cant set the height of my uitableviewcell right

I set the size of my UITableCellĀ“s with this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * vergleich = [nachricht objectAtIndex:indexPath.row];
CGSize size = [vergleich sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]
constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
return size.height + 30;
}
nachricht is a NSArray which contains all the messages. The code just looks how long the message (with a specified font) is and calculate the height. I set + 30, because over the message(UITextView) is a UIlabel.
The UITextView, which should contain the messages, get the size with this code:
- (void)setTweetText:(NSString *)_tweet;{
CGSize size = [_tweet sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14]
constrainedToSize:CGSizeMake(268, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
[textText setFrame:CGRectMake(55, 25, 268, size.height + 10)];
textText.text = _tweet;
[textText sizeToFit];
textText.dataDetectorTypes = UIDataDetectorTypeLink;
}
Now there is a problem and I don't know why: The UITextView is bigger then the cell, even if I set the size of the cell height there is a unpleasant distance between the TextView and the next cell. Why doesn't he get the right height for some cells. Here is an example:
alt text http://img34.imageshack.us/img34/214/bildschirmfoto20100120uw.png
All I can say is in my cellForRowAtIndexPath I use the following and it works. My heightForRowAtIndexPath is nearly identical to yours.
double d = [self tableView:table heightForRowAtIndexPath:indexPath];
UILabel* label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 280, d-20)] autorelease];
label.numberOfLines = 100;
label.lineBreakMode = UILineBreakModeWordWrap;
[label setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
label.text = [descriptions objectAtIndex:indexPath.section];
[cell.contentView addSubview:label];
one possibility is you have set the CGSize width to be different in each function, for row height you have CGSizeMake(268, MAX_HEIGHT) and for setTweetText it is CGSizeMake(262, MAX_HEIGHT)
its not a good idea to sizeWithFont from that method, its fairly heavy and will chop up your scrolling performance. if possible calculate the heights before and store them for quick access from that method