2017-01-26 86 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的高度。

希望它会帮助你... :)