1

ステップ 1 - これは正常に動作しています

カスタム UITableViewCells をロードする UITableView があります

ステップ 2 - これはちょっとした作業です

UITableViewCell の高さを変更して、UITextView 内のセルに含まれるすべてのデータが表示されるようにします

次のコードを使用して、UItextView データを取得し、サイズを変更しました。

UITextView *dummy = [[UITextView alloc] init];
dummy.font = [UIFont systemFontOfSize:14];
dummy.text = cell.textView.text;

CGSize newSize = [dummy sizeThatFits:CGSizeMake(270.0f, 500.0f)];

//get the textView frame and change it's size
CGRect newFrame = cell.textView.frame;
newFrame.size = CGSizeMake(270.0f, fmaxf(newSize.height, 60));
cell.textView.frame = newFrame;

//resize the cell view I add +95 because my cell has borders and other stuff...
CGRect cellFrame = CGRectMake(0, 0, 320, newFrame.size.height+95);
cell.cellView.frame = cellFrame;

次に、デリゲート関数 heightForRowAtIndexPath を使用して UITableViewCell の高さを設定します。

コードを実行して表のセルを上下にスクロールすると、動作が常に期待どおりになるとは限りません...つまり、セルのサイズが常に正しいサイズであるとは限りませんが、上下にスクロールすると、右にロードされることがありますサイズをもう一度。おそらく dequeueReusableCellWithIdentifier が問題だと考えています

cellIdentifier = @"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

同じ識別子でサイズの異なるセルをリサイクルしているので...

この問題を解決するための最善の方法について、誰かがガイドラインを教えてもらえますか?

ありがとう!!!

セルが最初にロードされたときのスクリーンショット 1

スクロール後のスクリーンショット 2 一番上のセルが修正されていることに注意してください

4

2 に答える 2

0

次の方法を実装しましたか?

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath :

互いに異なる場合は、各行の高さを設定する必要があります

于 2013-10-24T15:00:31.127 に答える
0

cellForRowAtIndexPath メソッド内に UITextView を動的に追加することから始めます。assumes(データ配列には、セル内に表示されるコンテンツが含まれています) // 定義します

#define CELL_TEXTVIEW_WIDTH = 320        
#define CELL_TEXTVIEW_HEIGHT = 9999  
#define PADDING = 5

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"tableCell";

ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if(cell == nil){

cell =  [[tableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

 NSString *_data = [data objectAtIndex:indexPath.row];

CGSize _data_contentsize  = [_data sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(CELL_TEXTVIEW_WIDTH, CELL_TEXTVIEW_HEIGHT) lineBreakMode:NSLineBreakModeWordWrap];

UITextView *dummy=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, _data_contentsize +(PADDING * 2))];

// add Font and text color for your textview here

[cell.contentView addSubview:dummy];

return cell;

}

この後、heightForRowAtIndexPath メソッドでセルの高さを計算します。

于 2013-10-24T18:02:18.497 に答える