動的計算UItableViewCellの高さ

5689 ワード

動的計算UItableViewCellの高さ
UILabel in UITableViewCell
AutoLayout-UILabelのプロパティLinesを0に設定して複数行を表示します.AutoLayout制約は必ず完全に確立しなければならない-UItable ViewはC 1を使用する.xibでカスタマイズされたCellは、UItableViewに登録する必要があります.
 UINib *cellNib = [UINib nibWithNibName:@"C1" bundle:nil]; 
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"C1"]; 
  • データソースメソッド``
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {//Return the number of rows in the section. return self.tableData.count; }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { C1 cell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"]; cell.t.text = [self.tableData objectAtIndex:indexPath.row]; return cell; } ```
  • は、UItableViewCellが占有する空間の高さを計算するために、新しいAPI systemLayoutSizeFittingSize:を使用する必要があります.Cellの高さは-(CGFloat)tableView:(UItableView)tableView heightForRowAtIndexPath:(NSIndexPath)indexPathというUItableView Delegateの方法でUItableViewに伝わります.
  • ここには特に注意すべき問題があります.つまり、 の問題です.UITAbleViewは全てのCellの高さを一度に計算したものであり、1 W個のCellがあれば-(CGFloat)tableView:(UITAbleView)tableView heightForRowAtIndexPath:(NSIndexPath)indexPathは1 W回トリガーされてコンテンツが表示される.しかし、iOS 7以降、この1 Wコールを回避する新しい方法が提供されている.これは、(CGFloat)tableView:(UItableView)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath)indexPathである.Cellの推定値を返すことが要求され、表示されたCellだけが計算高さのprotocolをトリガするこの方法が実現された.システムLayoutSizeFittingSizeはcellのインスタンスを計算する必要があるため、ここではメンバー変数でCellの実列を保存します.これにより、Cellの高さを計算するたびにCellインスタンスを動的に生成する必要がなくなり、便利で効率的でメモリを少なくすることができます.一挙三得です.
  • は、Cellの高さを計算するインスタンス変数
     @property (nonatomic, strong) UITableViewCell *prototypeCell; 
  • を宣言する.
  • 初期化prototypeCell
    self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"]; 
  • 計算Cell高さの実現``
  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath { C1 cell = (C1 )self.prototypeCell; cell.t.text = [self.tableData objectAtIndex:indexPath.row]; CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; NSLog(@"h=%f", size.height + 1); return 1 + size.height; } ```
  • UITableViewCellの高さはcontentViewより1高いです.つまり、その区切り線の高さです.##Manual Layout``
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { C3 cell = [self.tableView dequeueReusableCellWithIdentifier:@"C3"]; cell.t.text = [self.tableData objectAtIndex:indexPath.row]; [cell.t sizeToFit]; return cell; }
  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath { C3 cell = (C3 )self.prototypeCell; NSString *str = [self.tableData objectAtIndex:indexPath.row]; cell.t.text = str; CGSize s = [str calculateSize:CGSizeMake(cell.t.frame.size.width, FLT_MAX) font:cell.t.font]; CGFloat defaultHeight = cell.contentView.frame.size.height; CGFloat height = s.height > defaultHeight ? s.height : defaultHeight; NSLog(@"h=%f", height); return 1 + height; }//NSStringのカテゴリ
  • (CGSize)calculateSize:(CGSize)size font:(UIFont )font { CGSize expectedLabelSize = CGSizeZero; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { NSMutableParagraphStyle paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy}; expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; } else { expectedLabelSize = [self sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping]; } return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height)); }
    ## UITextView in UITableViewCell
     ### Auto Layout -         
  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath { C2 cell = (C2 )self.prototypeCell; cell.t.text = [self.tableData objectAtIndex:indexPath.row]; CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; CGSize textViewSize = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)]; CGFloat h = size.height + textViewSize.height; h = h > 89 ? h : 89;//89は画像表示の最低高さであり、xib NSLog(@「h=%f」、h)を参照.return 1 + h; }
    > `  :`  sizeThatFits:   UITextView   (    UITextView          ),    systemLayoutSizeFittingSize:     。       ?   UITextView         systemLayoutSizeFittingSize  。 ![](example.jpg) >          10,      8,       87,       13,   systemLayoutSizeFittingSize:   CGSize height  19, size  100.  UITextView frame    systemLayoutSizeFittingSize:   。  ,    textViewSize.height 
    ### Manual Layout 
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { C4 cell = [self.tableView dequeueReusableCellWithIdentifier:@"C4"]; cell.t.text = [self.tableData objectAtIndex:indexPath.row]; [cell.t sizeToFit]; return cell; }
  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath { C4 cell = (C4 )self.prototypeCell; NSString *str = [self.tableData objectAtIndex:indexPath.row]; cell.t.text = str; CGSize s = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)]; CGFloat defaultHeight = cell.contentView.frame.size.height; CGFloat height = s.height > defaultHeight ? s.height : defaultHeight; return 1 + height; } ```