微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 在UITableViewCell内部动态查找UIWebView高度

我有一个这样的自定义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

原文地址:https://www.jb51.cc/c/115505.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐