我有一个这样的自定义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];
}
我可以成功地在UITableView中使用UITababVIEW单元格,称为主视图控制器。uitableviewcell的delagate是customquestioncell。在MeavVIEW控制器中,我调用下面的代码将值分配给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];
}
我的问题是,在使用TabelVIEW的MainViewController中,不能正确设置单元格的高度,它使用自定义单元格“CuutQualCycell”。在函数中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
在主视图控制器中,如何将单元格的高度设置为AWebVIEW.Fr.siZ.Head???是吗?
最佳答案
我认为最好的方法是创建一个委托。
一。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];
}
}
三。
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;
四。
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