// I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.
I want to fix height for cell based on HTML file height.
Note: loading HTML file will be different for every cell.(height is not constant for every HTML file)
// I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.
I want to fix height for cell based on HTML file height.
Note: loading HTML file will be different for every cell.(height is not constant for every HTML file)
To get the height of UIWebView
object, first you need to load them. Then inside the delegate method of UIWebView
you can get the height as per the html content like given below.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"%f",myWebView.scrollView.contentSize.height);
}
you can also get the height of UIWebView
by JS insertion like this
[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
In webViewDidFinishLoad
method you have to store height based on webview object tag.
After that load your table and in below method give height accordingly.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
If I understood correctly..
Here you sets cell.textLabel.lineBreakMode and number of lines, for cell.textLabel. (0 - infinity)
- (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.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
cell.textLabel.text = [news objectAtIndex:indexPath.row];
return cell;
}
Here you need to count the height of cell.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [news objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20.0f;
}
Implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self getItemForKey:kSummary];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
//You will need to define kDefaultCellFont
CGSize labelSize = [text sizeWithFont:kDefaultCellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + ANY_OTHER_HEIGHT;
}
If you want more check the Link