iPhone -> dynamic cell height - iphone

I have a UITableView and a Cell which I design with Interface Builder.
I have two labels there.
The problem is, that one of the labels is text which can be 500 words or 10 words.
So I need a dynamic cell height. How can I handle this, because otherwise I have empty space or the cell height is not enough.
Perhaps someone has an idea how to do this?
Best Regards.

To know the size of your text, you can use this method:
- (CGFloat)measureTextHeight:(NSString*)text fontName:(NSString*)fontName fontSize:(CGFloat)fontSize constrainedToSize:(CGSize)constrainedToSize {
CGSize mTempSize = [text sizeWithFont:[UIFont fontWithName:fontName size:fontSize] constrainedToSize:constrainedToSize lineBreakMode:UILineBreakModeWordWrap];
return mTempSize.height; }
Then you can set your size cell with:
-(CGFloat)tableView:(UITableVIew*)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath;

-(CGFloat)tableView:(UITableVIew*) tableView heightForRowAtIndexPath(NSIndexPath *) indexPath
{
//here u can check the row number and ur labels and return ur custom height.
}

Related

Detailed view controller in Objective-C

I'm having some trouble understanding how I should implement a detailed view controller for a table view, when I don't know how long/short the content is going to be.
Think of an RSS app. The main table view shows all the items, and when you click, you're supposed to get the contents of that item/article. How could I solve this using table views for the application, when table cells have a static height?
I'm using storyboard and segues for the application.
For your detail view you could place some or all of your content in a UIScrollView.
By the way, table cell heights are not static - just implelemt heightForRowAtIndexPath to calculate the height from your model data. If the cell contains text you will need to calculate the size given the required font. (Theres an answer on how to do this on StackOverflow here )
you can use following method to calculate dynamic height of row and lable as well.Use it in detail view.I think it will resolve your query.
Use it in heightForRowAtIndexPath for setting cell height and in cellForRowAtIndexPath for setting height of lable.
-
(CGFloat) GetHeightFoText:(NSString *)aStrTxt FoWidth:(int)aIntWidth ForFontSize:(int)aIntFntSize
{
CGSize maximumLabelSize = CGSizeMake(aIntWidth,9999);
CGSize expectedLabelSize = [text sizeWithFont:[UIFont systemFontOfSize:aIntFntSize]
constrainedToSize:maximumLabelSize
lineBreakMode:UILineBreakModeWordWrap];
return expectedLabelSize.height;
}

Within cellForRowAtIndexPath getting height of cell when cells are variable height

I create custom cells within my tableview some have images and are tall some are just text. The height of the cells are calculated in heightForRowAtIndexPath, which I beleive is done before cellForRowAtIndexPath is called. I want to place an imageview at the bottom of the cell regardless of heigh, but I am not sure how to get the calculated height from within cellForRowAtIndexPath?
Too late for an answer..
But, like #user216661 pointed out, the problem with taking the height of the Cell or the ContentView is that it returns the cells original height. Incase of rows with Variable height, this is an issue.
A better solution is to get the Rect of the Cell (rectForRowAtIndexPath) and then get the Height from it.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
You can ask the delegate, but you'll be asking twice since the tableView already asks and sizes the cell accordingly. It's better to find out from the cell itself...
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
You are allowed to call heightForRowAtIndexPath yourself! Just pass the indexPath from cellForRowAtIndexPath as an argument and you can know the height of the cell you are setting up.
Assuming you are using a UITableViewController, just use this inside cellForRowAtIndexPath...
float height = [self heightForRowAtIndexPath:indexPath]

Get width/frame of entire UITableViewCell

I have a method where I have a (rendered) UITableViewCell*. I would like to know the width/frame of the entire cell.
cell.frame doesn't seem to work - it gives me the frame of a random cell. The closest I got is cell.contentView.frame but it doesn't include the area covered by the accessoryView (see http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7). cell.contentView.superview.frame doesn't work either - it gives me a frame at some arbitrary location just like cell.frame does.
cell.contentView.width + cell.accessoryView.width works except in cases where the accessory view is UITableViewCellAccessoryNone
Any ideas how I can get the entire frame/width of the UITableViewCell in all cases?
Try this,
NSLog(#"Cell Width = %f",cell.frame.size.width);
NSLog(#"Cell Height = %f",cell.frame.size.height);
It will show the current cell Width and Height.
This code gives you the frame of the particular cell selected by indexPath:
// Get the cell rect and adjust it to consider scroll offset
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
cellRect = CGRectMake(cellRect.origin.x - tableView.contentOffset.x, cellRect.origin.y - tableView.contentOffset.y, cellRect.size.width, cellRect.size.height);
cell.frame will always give you the frame of the cell you're messaging.
And even if it did return a random cell, you should still be able to use the width of the frame, because all the cells have the same width.
I think by default, a cell will use 100% of the width of the UITableView so a UITableView's width is equivalent to a cell's width and because you would likely have set your table cell height using:
[UITableView setRowHeight:tableCellHeight];
Where tableCellHeight is a number of your choice and UITableView is the name of your UITableView not the UITableView class itself.
Then you would have both the width and height of a table cell, or did I misinterpret your question?

Adjusting row height correctly

Showing dynamic data in table cell make problem for me. I am using
[titleString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(285,9999) lineBreakMode:UILineBreakModeWordWrap];
function for calculating the size now according to size i need to adjust row sizes. but it is inconsistent, Some time it give size(242,18) and (40,18) (showing width,height). when it gives more width for same height the it breaks in two line text and inconsistency begins. if i take less height then it overflow the text and if less in height then some time left a huge white space.
please help me and suggest some proper way for doing this.
you code seem me correct , there is only one place for modification left in your code that would be use CGFLOAT_MAX instead of 9999.
And Also check the lineBreakMode property, Assign numberOfLines With zero.
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
There is delegate method as follows
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == theRowWhereUwantToIncreaseTheSize)
{
return 105.0f;;
}
}

Automatic tableView cell size in iPhone?

I want to make a UITableView which automatically adjust it's cell size by it's contents.
The contents are from XML file and the data is just text.
I want to put the text in a cell and it should be automatically does word-wrap and support multi-line.
So each cell can have different height. Is this possible?? or any idea?
Thank you ;)
Yes it is possible. Make use of the following method for doing it,
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
CGSize cellHeight;
if (indexPath.row == 0) {
cellHeight = [yourXMLContents sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
return cellHeight.height + 20;
} else
return 30;
}
Have you looked at the three20 library? It'll resize the cells to fit the content. In the very least, you can look at how the code is implemented to resize cells.