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 ????
The best way (I think) is create a delegate.
1. CustomQuestionCell.h
@protocol CustomQuestionCellDelegate;
@interface CustomQuestionCell : UITableViewCell {
IBOutlet UIWebView *mywebView;
id <CustomQuestionCellDelegate> *delegate;
}
- (void)checkHeight;
@property (nonatomic, assign) id <CustomQuestionCellDelegate> *delegate;
@end
@protocol CustomQuestionCellDelegate <NSObject>
@optional
- (void)customQuestionCell:(CustomQuestionCell *)cell shouldAssignHeight:(CGFloat)newHeight;
@end
2. CustomQuestionCell.m (webViewDidFinishLoad:)
- (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];
}
- (void)checkHeight {
if([self.delegate respondsToSelector:@selector(customQuestionCell:shouldAssignHeight:)]) {
[self.delegate customQuestionCell:self shouldAssignHeight:aWebView.frame.size.height];
}
}
3. MainViewController.m (tableView:cellForRowAtIndexPath:)
CustomQuestionCell *cell = (CustomQuestionCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CustomQuestionCellView" owner:self options:nil];
cell = tblQuestionCell;
}
cell.delegate = self; // <-- add this
[cell AssignWebView:[ListOfQuestions objectAtIndex:indexPath.row]];
if([[didReloadRowsBools objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]] boolValue] != YES) {
[cell checkHeight];
}
return cell;
4. MainViewController.m (add theese methods wherever you want)
- (void)customQuestionCell:(CustomQuestionCell *)cell shouldAssignHeight:(CGFloat)newHeight {
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
[cellHeights setObject:newHeight forKey:[NSString stringWithFormat:@"%d",indexPath.row]];
[didReloadRowsBools setObject:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d",indexPath.row]];
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [NSString stringWithFormat:@"%d",indexPath.row];
if([cellHeights objectForKey:key] == nil) {
return 44; // change it to your default height
} else {
return [cellHeights objectForKey:key];
}
}
5. MainViewController.h
#import "CustomQuestionCell.h"
@interface MainViewController : UIViewController <CustomQuestionCellDelegate> {
NSMutableDictionary *cellHeights; // <-- add this
NSMutableDictionary *didReloadRowsBools; <-- and this
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With