加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:53:17 所属栏目:百科 来源:网络整理
导读:我有一个这样的自定义UITableViewCell // CustomQuestionCell.h@interface CustomQuestionCell : UITableViewCell { IBOutlet UIWebView *mywebView;}-(void) AssignWebView:(NSString *) _text;// CustomQuestionCell.m-(void) AssignWebView:(NSString *)
我有一个这样的自定义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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读