2017-01-26 94 views
1

我有一個相當標準的UITableView通過自定義單元填充。那個單元現在只是一個圖像和一個標籤。在我的生活中,我無法調整它自己。無法讓UITableViewAutomaticDimension正常工作

當我包含UITableViewAutomaticDimension時,除了不正確的佈局外,我無法填充我的數據。

沒有UITableViewAutomaticDimension,數據顯示正常。

使用SnapKit處理約束是和流星/ SwiftDDP來處理數據,但有另一種的UITableView中,這似乎是正確

工作的ViewController

class CommentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    var commentTable:UITableView! 
    var comments:MeteorCollection<Comment>! 

    init() { 
     super.init(nibName: nil, bundle: nil) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     comments = MeteorCollection<Comment>(name: "comments") 

     createView() 

     Meteor.subscribe("postsComments", params: [["postId": self.source!.id!]]) {} 
    } 

    func createView() { 
     let contentTableView = UITableView(frame: content.frame) 
     content.addSubview(contentTableView) 
     contentTableView.backgroundColor = UIColor.clearColor() 
     self.commentTable = contentTableView 
     contentTableView.delegate = self 
     contentTableView.dataSource = self 

     contentTableView.snp_makeConstraints { (make) -> Void in 
      make.top.equalTo(content) 
      make.left.equalTo(content) 
      make.right.equalTo(content) 
      make.height.equalTo(content).inset(65) 
     } 

     contentTableView.rowHeight = UITableViewAutomaticDimension 
     contentTableView.estimatedRowHeight = 350 

    } 
} 

項目評論TableViewDelegate.swift

import UIKit 

extension CommentViewController { 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.comments.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(CommentTableViewCell.reuseIdentifier, forIndexPath: indexPath) as UITableViewCell 

     cell.setNeedsUpdateConstraints() 
     cell.updateConstraintsIfNeeded() 
     if let card = cell as? CommentTableViewCell { 
      let item = self.comments.sorted[indexPath.row] 
      card.populate(item) 
     } 

     return CommentTableViewCell() 
    } 

    func reloadTableView() { 
     self.commentTable.reloadData() 
    } 
} 

在使用UITableViewAutomaticDimension enter image description here

我亂碼混亂的一個例子使用UITableViewAutomaticDimension enter image description here

+0

請將contentTableView.estimatedRowHeight = 1個//估計值,如果小於正常工作 –

回答

1

當我不知道,如果它的工作原理,但我有一個我的亂碼混亂的例子最近經歷了同樣的問題,我通過更改estimatedRowHeight來修復它。

可以請你試一次: -

contentTableView.estimatedRowHeight = 350 

到,contentTableView.estimatedRowHeight = 160

+0

這並沒有這樣的伎倆。你在哪裏把你的估計的羅高線?也許它們與我的不同。 –

+0

你確定約束嗎? 是啊,我一直在使用這個函數,通過創建** tableview **和 'tableview.estimatedRowHeight'的出口,同時viewDidAppear。 – Aashish

4

這可能是由於細胞內的不當限制。請從故事板的屬性檢查器中部分正確的表格視圖單元格添加約束並設置UILable的這兩個屬性:

  • 線0
  • 斷行字包裹

,或者您也可以設置這些屬性代碼:

self.lblxyz.numberOfLines = 0 
self.lblxyz.lineBreakMode = .byWordWrapping 

注 - 不要修復UILable的高度。

希望它會幫助你... :)