2016-06-28 31 views
0

這是我的問題。我使用一個自定義的UITableViewCell作爲一個帶有標識符「cellidentifier」的Cell原型放入UITableView中。在這個UITableViewCell中,我添加了一個空的UIView到contentView的單元格中,我稱其爲「bubbleView」,它有一個連接@IBOutlet,並且我添加了約束top = 0,bottom = 0,leading = 0和trailing = 0。iOS + Swift 2:UITableViewCell中的動態高度不適用於空視圖

在控制器(其從UITableViewDataSource和的UITableViewDelegate繼承),放入viewDidLoad中功能添加此行:

override func viewDidLoad() { 
    ... 
    self.mytable.delegate = self 
    self.mytable.dataSource = self 
    self.mytable.estimatedRowHeight = 100.0 
    self.mytable.rowHeight = UITableViewAutomaticDimension; 
    ... 
} 

然後,我實現這種方式的UITableViewDelegate方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cellidentifier") as! myCustomCell! 
    if (cell == nil) { 
     cell = myCustomCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"cellidentifier") 
    } 
    let myNewView = UIView(frame: CGRectMake(0, 10, randomWidth, random Height)) 
    cell.bubbleView.addSubview(myNewView) 
    return cell 
} 

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

有了這些代碼,我可以將myNewView添加到單元格中,但單元格未根據myNewView高度調整到實際高度,例如,如果myNewView高度爲134,則我的自定義視圖單元格總是返回44高度。按照一些教程的說法,我添加了layoutIfNeeded屬性,但它不起作用。任何人都有這個解決方案?

回答

0

您的空視圖是否有高度限制?如果您只是將氣泡視圖固定在超級視圖的邊緣,我認爲這不足以讓Auto Layout找出單元格的高度。

此外,對於您的原型單元格,請轉至大小檢查器。你的表格視圖單元在那裏配置如何?將其配置爲「默認」與「自定義」可能會或可能無法解決問題。

如果你想讓單元真正自動自動化,你肯定需要提供足夠的約束,以便實際計算高度。

0

解決方案:這是一個關於約束的話題。如果您在Interface Builder中注意到,您需要對約束條件非常清楚,因此爲空視圖分配頂部,底部,前導和尾隨都是模糊的。然後,將溶液是添加約束根據你的需要(在我的上頂端,寬度和高度的情況下),並設置高度約束在參考以查看容器(cell.bubbleView)與myNewView到myNewView:

//Create my view 
let myNewView = UIView(frame: CGRectMake(0, 10, randomWidth, random Height)) 

myNewView.translatesAutoresizingMaskIntoConstraints = true 

//Assign top constraint according with container (cell.bubbleView) 
let pinTop = NSLayoutConstraint(item: myNewView, attribute: .Top, relatedBy: .Equal, toItem: cell.bubbleView, attribute: .Top, multiplier: 1.0, constant: 10) 
cell.bubbleView.addConstraint(pinTop) 

//set width constraint of myNewView 
let pinWidth = NSLayoutConstraint(item: myNewView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.width) 
cell.bubbleView.addConstraint(pinWidth) 

//set height constraint of myNewView 
let pinHeight = NSLayoutConstraint(item: myNewView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.height) 
cell.bubbleView.addConstraint(pinHeight) 

//set height constraint of container according the myNewView plus top and bottom space 
let pinHeightView = NSLayoutConstraint(item: cell.bubbleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: myNewView.frame.size.height+20) 
cell.bubbleView.addConstraint(pinHeightView) 

cell.bubbleView.addSubview(myNewView) 

最後唐忘了配置UITableView來動態創建高度單元格。

self.mytable.estimatedRowHeight = 100.0 
self.mytable.rowHeight = UITableViewAutomaticDimension; 

而且它完美

相關問題