///I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.
HTML ファイルの高さに基づいてセルの高さを固定したい。
注: HTML ファイルの読み込みはセルごとに異なります (高さは HTML ファイルごとに一定ではありません)。
///I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.
HTML ファイルの高さに基づいてセルの高さを固定したい。
注: HTML ファイルの読み込みはセルごとに異なります (高さは HTML ファイルごとに一定ではありません)。
UIWebView
オブジェクトの高さを取得するには、まずそれらをロードする必要があります。次に、デリゲート メソッド内で、UIWebView
以下のような html コンテンツに従って高さを取得できます。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"%f",myWebView.scrollView.contentSize.height);
}
UIWebView
このように JS を挿入して高さを取得することもできます
[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
webViewDidFinishLoad
メソッドでは、webview object tag に基づいて高さを保存する必要があります。
その後、テーブルをロードし、以下のメソッドでそれに応じて高さを指定します。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
私が正しく理解していれば..
ここでは、cell.textLabel の cell.textLabel.lineBreakMode と行数を設定します。(0 - 無限大)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
cell.textLabel.text = [news objectAtIndex:indexPath.row];
return cell;
}
ここでは、セルの高さを数える必要があります。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [news objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20.0f;
}
UITableViewDelegate メソッドを実装します - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self getItemForKey:kSummary];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
//You will need to define kDefaultCellFont
CGSize labelSize = [text sizeWithFont:kDefaultCellFont
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + ANY_OTHER_HEIGHT;
}
詳細が必要な場合は、リンクを確認してください