2016-05-05 1 views
1

テーブルビューを含むビューコントローラがあります。テーブルビューは、単一ラベルを含むカスタムセルで構成されています。私はそれらの細胞に表示される様々な長さのテキストを持っています。私が直面している問題は、細胞が適切な高さに拡張されていないということです。私はSOにある多くのソリューションを試しましたが、今のところこれまで働いている人はいません。ここ ビューコントローラiOS 7のダイナミックテーブルビューセルの高さ

class ViewController: UIViewController { 

    @IBOutlet var tableView: UITableView! 

    let items = [ 
     "This is the first text", 
     "This is the first text and this is the second text","now you may be thinking where is the third text?. Well, There was the first text and second text, now here is the third text", 
     "This is the fourth short and sweet text", 
     "Slow down you crazy child, you're so ambitious for a juvenile. If you're so smart, tell me why are you still so afraid.","Where's the fire? What's the hurry about. You better cool it off before you burn it out. There's so much to do and so many hours in a day.You got your passion, got your pride. Don't you know that only fools are satisfied. Dream on but don't imagine that they come true. Don't even realise vienna waits for you"] 

    var prototypeCell:CustomCell! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //setting up tableview 
     self.tableView.allowsSelection = false 
     self.tableView.dataSource = self 
     self.tableView.delegate = self 

     configurePrototypeCell() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func configureCell(cell:CustomCell, forIndexPath indexPath:NSIndexPath) 
    { 
     cell.itemLabel.text = items[indexPath.row] 

    } 


    func configurePrototypeCell() 
    { 
     self.prototypeCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell 
    } 


} 

extension ViewController:UITableViewDataSource 
{ 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell 
     configureCell(cell, forIndexPath: indexPath) 
     return cell 

    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items.count 
    } 
} 

extension ViewController: UITableViewDelegate 
{ 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

     self.prototypeCell.itemLabel.text = items[indexPath.row] 
     self.prototypeCell.itemLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tableView.frame) 

     self.prototypeCell.setNeedsLayout() 
     self.prototypeCell.layoutIfNeeded() 


     let size = self.prototypeCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) 
     let height = size.height 
     return height 


    } 

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

} 

CustomCellクラスのコードであるUITableViewCellのサブクラスです。 itemLabelという名前のUILabelが含まれています。

+0

設定itemLabelの番号LIne = 0と改行モードを単語の折り返しにしてUITableviewAutomaticDimention –

+0

'iOS 7'は' Swift'をサポートしていますか? – AechoLiu

+1

@AechoLiuはいそれはあります – idocode

答えて

3

カスタムセルの高さを計算するには、高さのTableviewDelegateメソッドからこれをカスタムセルの呼び出しで試してください。

コード:
let size = sizingCell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

+0

ありがとう!私はprototypeCellをやっていた。 ** contentView ** .systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)です。 – idocode

0
override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.estimatedRowHeight = 40; 
    tableView.rowHeight = UITableViewAutomaticDimension; 
} 

これを有効にすると、制約を適切に設定するAutolayoutが必要です。

+1

これはiOS 8.0+でのみ動作しますが、iOS 7.0では解決策が必要です – idocode

0

ここでObjective-Cでコードを書いていますが、簡単に簡単に修正できます。

enter image description here

2)cellForRowでいくつかの変更を行い、あなたのcustomCell.mファイル

- (CGFloat)requiredHeight 
{ 
    CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
    NSLog(@"Size :%@",NSStringFromCGSize(size)); 
    CGFloat minimumHeight = 40.0f; //cell height which you have taken in xib 
    if (size.height < minimumHeight) 
    { 
     return minimumHeight; 
    } 
    return size.height; 
} 

3)に記載の方法の下に実装します。すべての

1)まず、このようなラベルの制約を設定しました次のようなHeightForRowメソッド:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.itemLabel.preferredMaxLayoutWidth = tableView.bounds.size.width -36; //Here 36 indicates Leading and Trailing distance of label which you have put in xib 
    [cell.itemLabel updateConstraintsIfNeeded]; 

    return cell; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    return [viewAllCell requiredHeight]+[self calculateLabelHeightforText:viewAllCell.Lblname.text andFonts:viewAllCell.Lblname.font andWidth:viewAllCell.Lblname.frame.size.width]-default label height; // 
} 
-(float)calculateLabelHeightforText:(NSString *)text andFonts:(UIFont *)font andWidth:(float)width 
{ 
    if (text==nil) { 
     return 40; //default height 
    } 
    NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:font}] 
    ; 
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} 
               options:NSStringDrawingUsesLineFragmentOrigin 
               context:nil]; 
    return rect.size.height+4; 
} 

4)のviewDidLoadでは、いくつかの変更を行います。

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.TblDetail.rowHeight = UITableViewAutomaticDimension; 
    self.TblDetail.estimatedRowHeight = 40.0; 
} 
0

あなたはboundingRect値を取得する場合の希望、そのが動的のUITableViewの高さを変更返し、その後、

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

      let str : String! = items[indexPath.row] 

      let boundingRect = str.boundingRectWithSize(CGSizeMake(tableView.frame.size.width - 100, CGFloat.max), 
       options:NSStringDrawingOptions.UsesLineFragmentOrigin, 
       attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 14.0)!], context:nil).size.height + 45.0 

      if(boundingRect > 95.0) { 
       return boundingRect 
      } else { 
       return 95 
      } 

    } 

、その私のために働いて、このコードを試してみてくださいその有用な

関連する問題