Automatic tableView cell size in iPhone? - 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.

Related

How to define the uitableviewcell height dynamically according by the text

I'm using a grouped uitableview. I have a lot of sections.
In the first section there is just one UITableViewCell. In this cell, I will show a text.
The problem is that I don't know the number of characters of the text and I would like to change the height of cell to show entire text.
can someone help me?
Thanks
in the UItableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath you can return height for specific row. For that you must know the length of the string you want to display in that row.
see this link Get tableView:heightForRowAtIndexPath: to happen after tableView:cellForRowAtIndexPath:?
You will have to use heightForRowAtIndexPath. Within that you will have to do all the calculations that you do in the cell's layoutsubviews or in cellForRowAtIndexPath too. For doing so you will have to figure out how long the text is. You will have to access the text in cellForRowAtIindexPath anyway. So why don't you know how long the text will be?
To determine the size of the actual text you can use NSSTring's sizeWithFont:forWidth:lineBreakMode:.
I have solved the problem applying the code below and creating a property numberOfTextRows.
(CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath
float height = 45.0f;
if (indexPath.section == 0 && indexPath.row == 0) {
CGSize theSize = [self.strResult sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
self.numberOfTextRows = round(theSize.height / 12);
if ((indexPath.section == height) || (self.numberOfTextRows < 2)) {
height = 45;
} else {
height = theSize.height + 16;
}
}
return height;
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;

Resizing UIWebView height to fit content

I'm loading an HTML string of arbitrary length into a UIWebView which is then displayed in a UITableViewCell. I do not want this UIWebView to scroll independently of the UITableView so I must resize its height to fit the content.
This UITableViewCell is also collapsable, not showing the UIWebView when it's in its collapsed state.
The rub is, how do I know what this height is so that I can have heightForRowAtIndexPath return a proper value and allow the table to look and scroll correctly?
Here's some example code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
return 286;
if (indexPath.row == 1) {
if (analysisExpanded)
return analysisWebViewHeight + 39;
else
return 52;
}
if (sourcesExpanded)
return sourcesWebViewHeight + 39;
return 53;
}
Pretty simple. The 39 is the height of some header stuff I have in the cell with the webview.
I am loading the "expanded view" of the cell from a nib, so to get the webview I'm calling viewForTag:
UIWebView* sourcesWebView = (UIWebView*)[cell viewWithTag:1];
[sourcesWebView setBackgroundColor:[UIColor clearColor]];
[sourcesWebView loadHTMLString:placeholder baseURL:[NSURL URLWithString:#"http://example.com/"]];
sourcesWebViewHeight = [[sourcesWebView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight"] floatValue];
[sourcesWebView setFrame:CGRectMake(sourcesWebView.frame.origin.x, sourcesWebView.frame.origin.y, sourcesWebView.frame.size.width, sourcesWebViewHeight)];
sourcesWebViewHeight is an iVar that is updated by the above code in cellForRowAtIndexPath. If the user taps the cell three times, expand-collapse-expand, and does a little scrolling, eventually it gets the correct height.
I tried "Preloading" the height by using a memory-only UIWebView object, but that never returned correct numbers for some reason.
The issue was that I was attempting to get the scroll size before the web view had a chance to render it.
After implementing the didFinishLoad delegate method, my original javascript worked fine to get the height.
For iOS Safari you need to use the following line:
NSInteger height = [[sourcesWebView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"] integerValue];
document.documentElement.scrollHeight is not working for this browser.
Just to be sure everything is right, you need to call this after your web-view has finished loading =)
UPDATE: another option, that will work only for iOS5, they've added scrollView property to the UIWebView, where you can get contentSize.
Since webViewDelegate can get the height of webView when HTML code is loaded,
the tableview:heightForRowAtIndexPath: method will enter an infinite loop for
trying to retrieve the webView located in the tableView Cell:
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIWebView *webContent = (UIWebView *) [cell viewWithTag:tagTableSingleContentWeb];
CGRect frame = webContent.frame;
height = frame.origin.y + frame.size.height + 30;
return height;
}
The above is the code I tried to change the tableView cell when I insert HTML code into the webView inside tableView cell, but it will cause infinite loop....
I am still trying to find out some other way.
p.s. WebViewDelegate way is ok when webView is located in ScrollView, but I do not how to make it work for inside TableView cell yet.
More and better answers to pretty much same question can be found here as well. Just thought it might be helpful for someone.

iPhone -> dynamic cell height

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.
}

how to resize UITableViewCell so it fits any detailTextLabel

im making a iphone app so that any text in the detailTextlabel will fit no matter what
for example...if the detailTextLabel is short it will show a normal size cell however it if is long it will show a bigger cell
thank you :D
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize maxSize = CGSizeMake(max_size);
CGSize cellSize = [yourString
sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:maxSize
lineBreakMode:UILineBreakModeWordWrap];
return cellSize.height;
}
Consider my implementation:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (theSection) {
case 0:
if (indexPath.row ==6) //the row you need to check for height (if any)
{
if([yourString length]<=35) // if less than 35 chars, just return a basic size of 50
{
return 50;
}
CGSize detailSize = [yourString sizeWithFont:[UIFont systemFontOfSize:20]constrainedToSize:CGSizeMake(270, 4000)lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height;
}
hope it helps, to help your understanding you may want to have a look at 'sizeWithFont' to understand what this is doing to return a size.
You have to use the tableView:heightForRowAtIndexPath: method on your UITableViewDelegate to return the height of the row. Note that this method should be very fast if you have more than a few rows, as it will be called for every row in the table rather than just the visible rows.

Can I have cells with different hight in an UITableView?

I need to present some information in a table view where some information is pretty flat, and other is pretty big. i.e. one information is just a line of text really, while the other is three lines of text.
From what I know, UITableView asks for the hight of all cells. So is there any way to have cells with distinct heights?
Yes, see:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Example implementation:
NSString *string...;
CGFloat CellWidth...;
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:CGSizeMake(textWidth, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
if (height + 13.0) < 44.0) {
return 44.0;
}
return height + 13.0;
I agree with paull, but you could also subclass if you wanted something extra than just resizing the height. Just think about what else you may need. Good luck, cocoa can be a real pain sometimes.