2017-04-18 101 views
0

我已經有這個問題已經有幾天了,仍然沒有找到修復。基本上我試圖創建一個UITableView自我大小的單元格。我已經設置了單元格高度爲UITableViewAutomaticDimension。單元格的高度受限制以測試我的代碼,因爲此約束條件應確定單元格的高度。下面是我對電池的約束:動態tableview單元格,防止創建自動高度約束

let views = ["view": view] 
self.addConstraints(NSLayoutConstraint.constraints(
    withVisualFormat: "H:|[view]|", 
    options: [], 
    metrics: nil, 
    views: views 
)) 

self.addConstraints(NSLayoutConstraint.constraints(
    withVisualFormat: "V:|[view]|", 
    options: [], 
    metrics: nil, 
    views: views 
)) 

NSLayoutConstraint(item: contentView, attribute: .width, relatedBy: .equal, toItem: self.superview!, attribute: .width, multiplier: 1, constant: 0).isActive = true 

,這裏是錯誤消息我得到:

2017-04-17 22:40:01.596805-0300 ViraVira-Info[52208:1001696] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x600000099190 h=--& v=--& UITableViewCellContentView:0x7f987070f5f0.height == 44.5 (active)>", 
    "<NSLayoutConstraint:0x6000000966c0 UIView:0x7f9870709be0.height == 100 (active)>", 
    "<NSLayoutConstraint:0x600000098dd0 V:|-(0)-[UIView:0x7f9870709be0] (active, names: '|':UITableViewCellContentView:0x7f987070f5f0)>", 
    "<NSLayoutConstraint:0x600000098e70 V:[UIView:0x7f9870709be0]-(0)-| (active, names: '|':UITableViewCellContentView:0x7f987070f5f0)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000966c0 UIView:0x7f9870709be0.height == 100 (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

正如你可以看到tableview自動創建一個height constraint這與我的其他高度約束衝突。

"<NSAutoresizingMaskLayoutConstraint:0x600000099190 h=--& v=--& UITableViewCellContentView:0x7f987070f5f0.height == 44.5 (active)>" 

但我找不到方法來刪除或防止發生這種約束。我已經嘗試將內容視圖的translatesAutoresizingMask設置爲false,將其自身的單元格視圖和要添加到內容視圖的UIView設置爲false,但未成功。這樣做給了我以下錯誤:

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a `tableview cell's` content view. We're considering the collapse unintentional and using standard height instead. 

請要求任何額外的信息,我會高興地把它添加到這個問題。

+0

view.translatesAutoresizingMaskIntoConstraints =假 應該幫助@ J.Paravicini – Shial

+0

你有紅色的問題?我說,我已經嘗試了,但沒有成功:/ –

+0

試試cell.contentView.translatesAutoresizingMaskIntoConstraints – Shial

回答

0

這是所有你需要做 - :

MARK-: But I did it programatically.This works perfect.

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    lazy var tableView : UITableView = { 
     var tableView = UITableView() 
     tableView.delegate = self // Table view delegate 
     tableView.dataSource = self // table view datasource 
     tableView.translatesAutoresizingMaskIntoConstraints = false // set translatesAutoresizingMaskIntoConstraints to false 
     return tableView 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     view.addSubview(tableView) // Add table on view 
     tableView.estimatedRowHeight = 50 // table row estimated row height 
     tableView.rowHeight = UITableViewAutomaticDimension //Automatic dimensions 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") // register cell 
     view.addConstraintsWithFormat(format: "H:|[v0]|", views: tableView) // give table view constraints horizontal 
     view.addConstraintsWithFormat(format: "V:|[v0]|", views: tableView) // vertical 

    } 

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


    // MARK-: Table view DELEGATE METHODS 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
     { 
     return 5 
    } 


    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = "abcd" 
     cell.textLabel?.numberOfLines = 0 // SET YOUR LABEL numberOfLines to 0 
     cell.textLabel?.sizeToFit() 
     if indexPath.row == 1 
     { 
      cell.textLabel?.text = "asdvhvcjkhsvcjhsvcjhsvchjvcjhsvcjhsvchdveiuvcjsdvccjgdcgygdchjsvcjsvcjvclsvccvsjvhjsvchsvcvhdchvsjvhcjjhhyuioplkjhxxzasdfghtyuiklopl" 
     } 
     return cell 
    } 


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 
     return UITableViewAutomaticDimension 
    } 


} 
extension UIView 
{ 

    func addConstraintsWithFormat(format:String,views:UIView...) 
    { 
     var allViews = [String:UIView]() 
     for data in 0...views.count-1 
     { 
      let key = "v\(data)" 
      allViews[key] = views[data] 
     } 
     addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: allViews)) 

    } 



} 

要與故事板的工作要做這個 - :

enter image description here

編碼兼職:

ViewCo ntroller-:

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    @IBOutlet weak var dynamicTable: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     dynamicTable.estimatedRowHeight = 70 // table row estimated row height 
     dynamicTable.rowHeight = UITableViewAutomaticDimension //Automatic dimensions 

    } 

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


    // MARK-: Table view DELEGATE METHODS 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
     { 
     return 5 
    } 


    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! testing 
     cell.automaticLabel?.text = "abcd" 
     cell.automaticLabel?.numberOfLines = 0 // SET YOUR LABEL numberOfLines to 0 
     cell.automaticLabel?.sizeToFit() 
     if indexPath.row == 1 
     { 
      cell.automaticLabel?.text = "asdvhvcjkhsvcjhsvcjhsvchjvcjhsvcjhsvchdveiuvcjsdvccjgdcgygdchjsvcjsvcjvclsvccvsjvhjsvchsvcvhdchvsjvhcjjhhyuioplkjhxxzasdfghtyuiklopl" 
     } 
     return cell 
    } 


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 
     return UITableViewAutomaticDimension 
    } 

自定義單元格講座:

import UIKit 

class testing: UITableViewCell { 

    @IBOutlet weak var automaticLabel: UILabel! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 
相關問題