本文介绍了在UITableViewCell内时动态查找UIWebView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的自定义UITableViewCell

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];

}

我可以在名为MainViewController的文件中的UITableView中成功使用UITableViewCell. UITableViewCell的派生类是CustomQuestionCell.在MainViewController中,我正在调用以下代码以将值分配给UIwebview.

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;

CustomQuestionCell.m中也有以下委托代码

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];
}

我的问题是我不能在MainViewController中正确设置单元格的高度,该MainViewController具有使用自定义单元格"CustomQuestionCell"的tableView.在功能

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

在MainViewController中,如何将单元格的高度设置为aWebView.frame.size.height ????

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

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:)

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:)

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)

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

5. MainViewController.h

#import "CustomQuestionCell.h"

@interface MainViewController : UIViewController <CustomQuestionCellDelegate> {

    NSMutableDictionary *cellHeights; // <-- add this
    NSMutableDictionary *didReloadRowsBools; <-- and this

}

@end

这篇关于在UITableViewCell内时动态查找UIWebView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 02:37