将整数存储为实例变量

时间:2012-11-12 01:24:36

标签: objective-c ios uitableview uiwebview

我在这一点上非常接近。基本上我要做的是返回UIWebView的单元格高度。我的NSLOG打印出正确的高度。现在的问题是我需要从那个高度做一个变量。到目前为止没有运气。这就是我现在所拥有的。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
_descriptionWebView.frame = CGRectMake(_descriptionWebView.frame.origin.x, _descriptionWebView.frame.origin.y, _descriptionWebView.frame.size.width, 1);
CGSize fittingSize = [_descriptionWebView sizeThatFits:CGSizeZero];
_descriptionWebView.frame = CGRectMake(_descriptionWebView.frame.origin.x, _descriptionWebView.frame.origin.y, _descriptionWebView.frame.size.width, fittingSize.height);

NSLog(@" Fitting Size is : %f", fittingSize.height);
}

它打印出将在UIWebView(UITableViewCell内)中显示的每篇文章的完美大小。我的主要问题是如何在 -

中返回该高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == SectionHeaderTitle) {

    // Regular
    return 54;

} else {

    return 612;
}
}

我试图用fittingSize创建一个实例变量,使用NSNumber,NSInteger,CGFloat,CGRect,CGSize ......基本上我得到“不兼容的结果类型”。

最终我想要的是return fittingSize.height;

到达那儿有什么帮助吗?非常感谢你提前。希望我没有遗漏任何东西!

3 个答案:

答案 0 :(得分:0)

CGSize.height是一个CGFloat。你在哪里得到“不兼容的结果类型”?

您应该能够在界面中声明CGFLoat变量CGFloat fittingHeight;,然后在-webViewDidFinishLoad中指定它:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
_descriptionWebView.frame = CGRectMake(_descriptionWebView.frame.origin.x, _descriptionWebView.frame.origin.y, _descriptionWebView.frame.size.width, 1);
CGSize fittingSize = [_descriptionWebView sizeThatFits:CGSizeZero];
_descriptionWebView.frame = CGRectMake(_descriptionWebView.frame.origin.x, _descriptionWebView.frame.origin.y, _descriptionWebView.frame.size.width, fittingSize.height);

fittingHeight = fittingSize.height;
}

然后在-tableView:heightForRowAtIndexPath:中使用它。

答案 1 :(得分:0)

中创建CGFloat类型的类级变量
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
      //Get the value of fittingsize.height
    }

在该变量中保存fittingsize.height的值,并在

中使用该变量
    -tableView:heightForRowAtIndexPath:indexpath
    {
      //Use the value stored of the CGFloat variable above
    }

答案 2 :(得分:-1)

强行将它投射到像这样的CGFloat中:

return (CGFloat)(fittingSize.height);