I have a custom UITableViewCell like this
// CustomQuestionCell.h
@interface CustomQuestionCell : UITableViewCell {
IBOutlet UIWebView *mywebView;
}
-(void) AssignWebView:(NSString *) _text;
// CustomQuestionCell.m
-(void) AssignWebView:(NSString *) _text {
[mywebView setDelegate:self];
[mywebView loadHTMLString:_text baseURL:nil];
}
I can successfully use the UITableViewCell in UITableView in file called MainViewController. The delagate of UITableViewCell is CustomQuestionCell. In MainViewController I am calling below code to assign value to UIwebview.
// cellForRowAtIndexPath
//CustomQuestionCel.xib uses the class CustomQuestionCell defined above.
CustomQuestionCell *cell = (CustomQuestionCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CustomQuestionCellView" owner:self options:nil];
cell = tblQuestionCell;
}
[cell AssignWebView:[ListOfQuestions objectAtIndex:indexPath.row]];
return cell;
I also have the following delegate code in CustomQuestionCell.m
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(@"webview frame size %f",aWebView.frame.size.height);
[aWebView setOpaque:NO];
[aWebView setBackgroundColor:[UIColor colorWithRed:249.0/255 green:243.0/255 blue:236.0/255 alpha:1.0]];
[activityLoad stopAnimating];
[mywebView setHidden:NO];
}
My problem is I am not able to set the the height of the cell properly in MainViewController which has tableView which uses the custom cell "CustomQuestionCell". In the function
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
in MainViewController how can I set the height of the cell as aWebView.frame.size.height ????
Best Answer
The best way (I think) is create a delegate.
1.
CustomQuestionCell.h
2.
CustomQuestionCell.m (webViewDidFinishLoad:)
3.
MainViewController.m (tableView:cellForRowAtIndexPath:)
4.
MainViewController.m (add theese methods wherever you want)
5.
MainViewController.h