iPhone Cells being drawn not showing full content till scroll - iphone

I've had a problem like this before but it was to do with the reuse of the cell. Now I know the cuprit line which i'll point out in the code below, but it's been added to make sure the cell text fits properly in the table (changing text sizes from XML feed)
The code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ViewRoutesCell * aCell = (ViewRoutesCell *)[tableView dequeueReusableCellWithIdentifier:#"ViewRoutesCell"];
if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:#"ViewRoutesCell" owner:self options:nil];
aCell = (ViewRoutesCell *)[arr objectAtIndex:0];
}
AssessObject *newObj1;
newObj1=[totalArray objectAtIndex:indexPath.row];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newObj1.routeImage]]];
aCell.imgRoute.image = image;
aCell.lblRouteText.text = newObj1.routeText;
aCell.lblRouteImage.text = newObj1.routeImage;
aCell.txtRoute.text = newObj1.routeText;
aCell.txtRoute.frame = CGRectMake(aCell.txtRoute.frame.origin.x, aCell.txtRoute.frame.origin.y, aCell.txtRoute.frame.size.width, aCell.txtRoute.contentSize.height);
return aCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
AssessObject *newObj1;
newObj1=[totalArray objectAtIndex:indexPath.row];
NSString *cellText = newObj1.routeText;
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:16.0];
CGSize constraintSize = CGSizeMake(320.0, CGFLOAT_MAX);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 250;
}
Now if i include the line:
aCell.txtRoute.frame = CGRectMake(aCell.txtRoute.frame.origin.x, aCell.txtRoute.frame.origin.y, aCell.txtRoute.frame.size.width, aCell.txtRoute.contentSize.height);
This happens (click for video)
However, when I comment that line out this happens:
Another short video
So it shows all the text at the start, but it's cutting the longer text sections out before all the text has been displayed (but making the cell the right height)
I'm very confused!
Tom

Are you setting up the txtRoute object's autoresizingMask property within your nib properly?

Related

Variable Height of TextView In TableView Cell

I have a basic UITableView, that I fill with a web service online, but I can't find a way to set the height of my cells (dynamic numbers of cells) according to the height of my textView.
Here's how I fill my cell :
UITextView *textView = (UITextView *)[cell viewWithTag:106];
textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
in my cellForRow methods :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I tried this but get an error of EXC_BAD_ACCESS
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PerleCell"];
UITextView *textView = (UITextView *)[cell viewWithTag:106];
textView.text = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGFloat *heightCell = (CGFloat*)textView.text.length;
return *heightCell;
}
In following method you can change height of your cell according to your textView.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// Here check length of your textView
// and after getting length return that length;
NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
Also in cellForRowAtIndexPath method calculate width of string in same manner and then give frame of width of yourtextView accordingly. After that add your textview on cell's contentView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// Create cell here by using any style and for this you can google some what that how to create cell in UITableView
// Here check length of your textView
// and after getting length return that length;
NSString *myString = [[stories objectAtIndex:indexPath.row] objectForKey:#"texte"];
CGSize maximumSize = CGSizeMake(300, 9999);
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,300,size.height)];
textView.text = myString;
[cell.contentView addSubView:textView];
return cell;
}

dynamic height of UITableViewCell depending upon the Text

I have a grouped tableview with a right detail cell layout. Now I got my data back from my server.
Sometimes it happens that I got back a very large text. So the tableviewCell should be higher than the others. You can see the screenshot of my problem over here.
Like you can see in my first cell I have a very large text. Now I want that the height of the cell grows with the length of the text, so i can always see the entire text instead off the '...' This is what I have in code at te moment.
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
EDIT
Code I have at the moment.
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, 400); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
And this is the result.
EDIT CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_label"];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
green:102/255.0
blue:102/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
return cell;
}
You need to use lineBreakMode:UILineBreakModeWordWrap in your code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
Moreover you need to set two properties of detailTextLabelalso in datasource methodcellForRowAtIndexPath` like this :
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
}
numberofLines property describes:
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
Try this:
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
CGSize constraintSize = CGSizeMake(330.0f, MAXFLOAT); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 20; }
Then, consider using unique cell identifiers. The idea here is that reusing the cell will result in mismatched heights for the contents. (Some people don't like the idea of unique identifiers, but they work and they're not problematic unless your table is going to be huge.) Accordingly, try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Subscription *sub = [_dictTask valueForKeyPath:#"subscription"];
NSArray *meta = sub.meta.allObjects;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"%i", indexPath.row]];
if (cell == nil){
cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_label"];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:#"sum_value"];
cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
green:102/255.0
blue:102/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
}
return cell;
}

UITableView Scrolling stuck when content/text of TableCell is big

Am creating a Rss feed app. Where i need to show Description (more than 5000 characters each). Am able to show all feeds in my app but my problem when i try to scroll the UITableView the scrolling not smooth i believe i need to make lazy loading for my content but am new to iphone development. Can any one show me correct way. And also set correct Cell Size according to the content size.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
if (item) {
if(cell.gestureRecognizers.count==0){
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longSelection:)];
longPress.minimumPressDuration=1.0;
[cell addGestureRecognizer:longPress];
}
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
cell.textLabel.textAlignment=NSTextAlignmentRight;
cell.textLabel.text=item.content;
cell.textLabel.numberOfLines=0;
cell.textLabel.font=[UIFont systemFontOfSize:15.0];
[cell.textLabel sizeToFit];
}
return cell;
}
Thanks
You have to use the -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
like so:
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
//make sure to add the font you're using
CGSize stringSize = [item.content sizeWithFont:[UIFont fontWithName:#"HelveticaNeue" size:14]
constrainedToSize:CGSizeMake(320, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
int rowHeight = stringSize.height;
return rowHeight;
}
Put this code:
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
Inside of here like so and remove it from where it is now (keeping it there causes you to have new imageviews created every time you scroll:
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image=[UIImage imageNamed:#"Background.png"];
imageView.image=image;
cell.backgroundView=imageView;
}

UITableView custom cell showing weird results?

UITableView custom cell showing weird results? I have my code below. Everything was showing fine until I put enough data to go off the initial screen then it started going crazy? My custom cell is shown below.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"customCell"; // This is identifier given in IB jason set this.
PostTableCustomCellController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"Cell created");
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"PostTableCustomCellController" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass:[PostTableCustomCellController class]])
{
cell = (PostTableCustomCellController *)currentObject;
}
}
}
Post *post = [postsArray objectAtIndex:indexPath.row];
cell.authorName.text = post.author;
cell.deadline.text = post.deadline;
cell.description.text = post.description;
Post *myPost = [postsArray objectAtIndex:[indexPath row]];
NSString *text = myPost.description;
// Configure the cell...
// [[cell authorName] setText:#"1 day"];
// [[cell distance] setText:#"Austin, TX"];
// [[cell description] setText:#"dd" ];
// Might can remove these
UILabel *locationLabel = (UILabel *) [cell distance];
UILabel *postTextView = (UILabel *) [cell description];
//CGSize maximumLabelSize = CGSizeMake(254,88.0f);
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 35.0f);
CGFloat realHeight = height + 36.0f - (10);
CGSize expectedLabelSize = [text sizeWithFont:postTextView.font
constrainedToSize:constraint
lineBreakMode:postTextView.lineBreakMode];
CGRect newFrame = postTextView.frame;
newFrame.size.height = expectedLabelSize.height;
postTextView.frame = newFrame;
[[cell description] sizeToFit];
CGRect locationRect = locationLabel.frame; // calls the getter
locationRect.origin.y = realHeight;
/* CGFloat locRectHeight = postTextView.bounds.size.height; */
/* locationRect.origin.y = cell.bounds.size.height; */
locationLabel.frame = locationRect;
//[[cell authorName] setText:#"jgervin"];
[[cell viewForBackground] sizeToFit];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
Post *myPost = [postsArray objectAtIndex:[indexPath row]];
NSString *text = myPost.description;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 35.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 36.0f;
}
You queing up old cells as they go off screen do to your dequeReusableCell, and the content and properties from the old cell must still be there. When you add content to a cell make sure to clear any previous properties so you can avoid these issues, or just turn off dequeReusableCell.
It looks like a cell-reuse problem. Why dont your turn off the dequeue-cell mechanism and see if the problem goes away. If it does, take a look at the code in your custom cell - are you creating multiple copies of the field labels? It looks that way.

Wrap words in uitableview, iphone [duplicate]

This is on iPhone 0S 2.0. Answers for 2.1 are fine too, though I am unaware of any differences regarding tables.
It feels like it should be possible to get text to wrap without creating a custom cell, since a UITableViewCell contains a UILabel by default. I know I can make it work if I create a custom cell, but that's not what I'm trying to achieve - I want to understand why my current approach doesn't work.
I've figured out that the label is created on demand (since the cell supports text and image access, so it doesn't create the data view until necessary), so if I do something like this:
cell.text = #""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];
then I get a valid label, but setting numberOfLines on that (and lineBreakMode) doesn't work - I still get single line text. There is plenty of height in the UILabel for the text to display - I'm just returning a large value for the height in heightForRowAtIndexPath.
Here is a simpler way, and it works for me:
Inside your cellForRowAtIndexPath: function. The first time you create your cell:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
}
You'll notice that I set the number of lines for the label to 0. This lets it use as many lines as it needs.
The next part is to specify how large your UITableViewCell will be, so do that in your heightForRowAtIndexPath function:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = #"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
}
I added 20 to my returned cell height because I like a little buffer around my text.
Updated Tim Rupe's answer for iOS7:
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:17.0];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = #"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:#
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 20;
}
A brief comment / answer to record my experience when I had the same problem. Despite using the code examples, the table view cell height was adjusting, but the label inside the cell was still not adjusting correctly - solution was that I was loading my cell from a custom NIB file, which happens after the cell height in adjusted.
And I had my settings inside the NIB file to not wrap text, and only have 1 line for the label; the NIB file settings were overriding the settings I adjusted inside the code.
The lesson I took was to make sure to always bear in mind what the state of the objects are at each point in time - they might not have been created yet! ... hth someone down the line.
If we are to add only text in UITableView cell, we need only two delegates to work with (no need to add extra UILabels)
1) cellForRowAtIndexPath
2) heightForRowAtIndexPath
This solution worked for me:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
cell.textLabel.text = [mutArr objectAtIndex:indexPath.section];
NSLog(#"%#",cell.textLabel.text);
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png" ]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelSize = CGSizeMake(200.0, 20.0);
NSString *strTemp = [mutArr objectAtIndex:indexPath.section];
if ([strTemp length] > 0)
labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
return (labelSize.height + 10);
}
Here the string mutArr is a mutable array from which i am getting my data.
EDIT :- Here is the array which I took.
mutArr= [[NSMutableArray alloc] init];
[mutArr addObject:#"HEMAN"];
[mutArr addObject:#"SUPERMAN"];
[mutArr addObject:#"Is SUPERMAN powerful than HEMAN"];
[mutArr addObject:#"Well, if HEMAN is weaker than SUPERMAN, both are friends and we will never get to know who is more powerful than whom because they will never have a fight among them"];
[mutArr addObject:#"Where are BATMAN and SPIDERMAN"];
Now the tableviews can have self-sizing cells. Set the table view up as follows
tableView.estimatedRowHeight = 85.0 //use an appropriate estimate
tableView.rowHeight = UITableViewAutomaticDimension
Apple Reference
I use the following solutions.
The data is provided separately in a member:
-(NSString *)getHeaderData:(int)theSection {
...
return rowText;
}
The handling can be easily done in cellForRowAtIndexPath.
Define the cell / define the font and assign these values to the result "cell".
Note that the numberoflines is set to "0", which means take what is needed.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *cellFont = [UIFont fontWithName:#"Verdana" size:12.0];
cell.textLabel.text= [self getRowData:indexPath.section];
cell.textLabel.font = cellFont;
cell.textLabel.numberOfLines=0;
return cell;
}
In heightForRowAtIndexPath, I calculate the heights of the wrapped text.
The boding size shall be related to the width of your cell. For iPad this shall be 1024.
For iPhone en iPod 320.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIFont *cellFont = [UIFont fontWithName:#"Verdana" size:12.0];
CGSize boundingSize = CGSizeMake(1024, CGFLOAT_MAX);
CGSize requiredSize = [[self getRowData:indexPath.section] sizeWithFont:cellFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
return requiredSize.height;
}
I found this to be quite simple and straightForward :
[self.tableView setRowHeight:whatEvereight.0f];
for e.g. :
[self.tableView setRowHeight:80.0f];
This may or may not be the best / standard approach to do so, but it worked in my case.
Try my code in swift . This code will work for normal UILabels also.
extension UILabel {
func lblFunction() {
//You can pass here all UILabel properties like Font, colour etc....
numberOfLines = 0
lineBreakMode = .byWordWrapping//If you want word wraping
lineBreakMode = .byCharWrapping//If you want character wraping
}
}
Now call simply like this
cell.textLabel.lblFunction()//Replace your label name
I think this is a better and shorter solution. Just format the UILabel (textLabel) of the cell to auto calculate for the height by specifying sizeToFit and everything should be fine.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = #"Whatever text you want to put here is ok";
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
[cell.textLabel sizeToFit];
return cell;
}
I don't think you can manipulate a base UITableViewCell's private UILabel to do this. You could add a new UILabel to the cell yourself and use numberOfLines with sizeToFit to size it appropriately. Something like:
UILabel* label = [[UILabel alloc] initWithFrame:cell.frame];
label.numberOfLines = <...an appriate number of lines...>
label.text = <...your text...>
[label sizeToFit];
[cell addSubview:label];
[label release];