我有一个这样的自定义UITableViewCell
// 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]; }
我可以在名为MainViewController的文件中的UITableView中成功使用UITableViewCell. UITableViewCell的delagate是CustomQuestionCell.在MainViewController中,我调用以下代码为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;
我还在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]; }
我的问题是我无法在MainViewController中正确设置单元格的高度,它具有使用自定义单元格“CustomQuestionCell”的tableView.在功能中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
在MainViewController中如何设置单元格的高度为aWebView.frame.size.height ????
解决方法
最好的方式(我想)是创建一个代表.
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