如何更改已加载的表格单元格的高度? [英] How do I change the height of a table cell that has already loaded?

查看:28
本文介绍了如何更改已加载的表格单元格的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在自定义的 uitableview 单元格中加载了 webviews.问题是 Web 视图的大小可变.在 webViewDidFinishLoad 中,我可以根据 html 文档的实际大小设置 web 视图的大小就好了.我的问题是表格单元格在 web 视图完成加载之前已经在 heightForRowAtIndexPath 中设置了高度.已加载表格单元格后,如何更改其高度?

Currently I have webviews loading in customized uitableview cells. The problem is the web views have variable sizes. In webViewDidFinishLoad I am able to set the size of the web view based on the actual size of the html document just fine. My problem is the table cells which have already had their height set in heightForRowAtIndexPath before the web views having finished loading. How can I change the height of a table cell after it has already been loaded?

理想情况下,我觉得我应该能够使用这样的一些代码行.

Ideally I feel like I should be able to use some line of code like this.

cellW.frame = cellW.cellWebView.frame;

但是我似乎无法访问 heightForRowAtIndexPath 中的单元格信息.我觉得我已经很好地解释了这种情况,但是您认为我应该发布的任何代码我都可以放在这里.我已经尝试了很多东西(所以到处都有评论和失败的尝试),但主要问题是我似乎无法在正确的位置访问单元格信息,例如 heightForRowAtIndexPath.如果我什至可以在 webViewDidFinishLoad 中以某种方式设置单元格信息,我可以简单地设置单元格的框架,我还可以在其中设置 Web 视图的框架大小.

However I don't seem to have access to cell information in heightForRowAtIndexPath. I've felt like I've explained the situation fairly well, but any code you think I should post I can put up here. I've tried a lot things (so there comments and failed attempts at this everywhere), but the main issue is I can't seem to access cell information in the right places such as heightForRowAtIndexPath. If I could even set cell information somehow in webViewDidFinishLoad, I could simply set the frame of the cell where I am also setting the frame size of the web view.

下面是我的表格单元子类的设置.

Below is the setup for my table cell subclass.

#import <UIKit/UIKit.h>
#import "DefinitionsAndConstants.h"

@interface UITableViewCellWebView : UITableViewCell
{
    UIWebView *cellWebView;
}

@property (nonatomic,retain) UIWebView *cellWebView;
@end

这是我上次尝试使用部分 Gavin 代码时的尝试.但当然,现在我已经离开了,因为 cellForRowAtIndexPath 不可分配,所以无法设置表格单元格.

Here is what I have tried last trying to use part Gavin's code. But of course there is no way to set the table cell now that I've gotten out because cellForRowAtIndexPath is not assignable.

-(void)webViewDidFinishLoad:(UIWebView *)webViews {
    [webViews stringByEvaluatingJavaScriptFromString:@"twitterfy()"];
    [webViews stringByEvaluatingJavaScriptFromString:@"document.getElementById('tweettext').innerHTML=tweet"];
    NSString* h = [webViews stringByEvaluatingJavaScriptFromString:@"getDocHeightMax()"];
    int height = [h intValue];
    webViews.frame = CGRectMake(0.0f, 0.0f, 320.0f, height);

    NSString* i = [webViews stringByEvaluatingJavaScriptFromString:@"return indexPath"];
    int ii = [i intValue];
    NSIndexPath* ip = [NSIndexPath indexPathForRow:ii inSection:0];

    UITableViewCellWebView *tableCell = (UITableViewCellWebView *)[self.tableView cellForRowAtIndexPath:ip];

    if (tableCell) {
        tableCell.frame = webViews.frame;
    }

    NSLog(@"Got height from webview %d", height);
}

推荐答案

我会设置一个可变数组来读取高度.在viewDidLoad中,你必须分配一些起始值(可能只是一堆@"44.0",其实无所谓,它不会被使用还很多).在您的 heightForRowAtIndexPath: 中,只需返回该数组中的数字.然后在 webViewDidFinishLoad 中,将数组中的高度替换为该单元格实际需要的高度(我相信您发布的代码中的 NSString *h),然后调用reloadData 在你的表格视图上.reloadData 将点击 heightForRowAtIndexPath:,它将查看 heights 数组,它现在具有该单元格所需的实际高度,所以一切都应该是闪亮的.

I would set up a mutable array to read heights from. In viewDidLoad, you'll have to assign some starting values (probably just a bunch of @"44.0", it doesn't really matter, it won't be used for much yet). In your heightForRowAtIndexPath:, just return the number from that array. Then in webViewDidFinishLoad, replace the heights in the array with the height you actually need for that cell (the NSString *h in the code you posted, I believe), and call reloadData on your table view. reloadData will hit heightForRowAtIndexPath:, which will look at the heights array, which now has the actual height needed for that cell, so everything should be shiny.

这篇关于如何更改已加载的表格单元格的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆