Лучший способ (я думаю) - создать делегата.
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