8

こんにちは、私の問題がありtableviewます。セルをuilabelで作成し、セットの高さをSizeToFit計算します。UILabeltableView

私のTableViewVell方法は次のとおりです。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [someArray objectAtIndex:indexPath.row];
// Set the UILabel to fit my text;
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
messageLabel.numberOfLines = 0;
[messageLabel sizeToFit];


return cell;   
}

セルの高さは正しいサイズのままUILabelです...これはビューをロードしたときの結果です:

最初のスクリーンショット

そして、これはTableViewのスクロールを開始した後です:

2 番目のスクリーンショット

何か助けて???

4

5 に答える 5

0
于 2014-11-18T11:43:23.413 に答える
0

自動レイアウトを使用していない場合は、カテゴリを追加することでこれを解決することもできますUIView

public func sizeToFit(constrainedSize: CGSize) -> CGSize {
  var newSize = sizeThatFits(constrainedSize)
  newSize.width = min(newSize.width, constrainedSize.width)
  newSize.height = min(newSize.height, constrainedSize.height)
  self.size = newSize
  return newSize
}

アイデアはsizeThatFits、制約されたサイズで使用して採用するサイズを決定し、次に予想される最小の幅/高さを使用することです。

次に、次のようなことをするだけです

yourLabel.sizeToFit(self.contentView.size) // or
yourLabel.sizeToFit(CGSize(width: maxWidth, height: self.contentView.size.height))
于 2016-01-21T10:11:32.867 に答える