【发布时间】:2015-04-28 18:06:34
【问题描述】:
我有一个带有表格视图的视图控制器。现在我想设置一个自我调整大小的单元格。单元格应包含三个或更多标签。 如果我只有两个标签,那么自动调整大小的单元格就可以完美地工作。但是一旦我添加第三个标签,第二个标签中的文本就不再换行了。第三个标签下方是很多可用空间。 Screenshot http://stefangerard.com/images/selfSizing.png 这是三个Label的约束。
- 第一个标签(Hello CustomCell)
constraint first Label http://stefangerard.com/images/constraint1.png
- 第二个标签(非常好奇...)
constraint second Label http://stefangerard.com/images/constraint2.png
- 第三个标签(最担心...)
constarint third Label http://stefangerard.com/images/constraint3.png
在我的 ViewController 中,我只是设置了单元格并在 viewDidLoad() 中调用这两行,单元格会自行计算其高度。
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
我的 ViewController 看起来像这样:
import UIKit
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var someRandomText1 = "Far curiosity incommode now led smallness allowance. Favour bed assure son things yet. She consisted consulted elsewhere happiness disposing household any old the. Widow downs you new shade drift hopes small. So otherwise commanded sweetness we improving. Instantly by daughters resembled unwilling principle so middleton. Fail most room even gone her end like. Comparison dissimilar unpleasant six compliment two unpleasing any add. Ashamed my company thought wishing colonel it prevent he in. Pretended residence are something far engrossed old off."
var someRandomText2 = "Concerns greatest margaret him absolute entrance nay. Door neat week do find past he. Be no surprise he honoured indulged. Unpacked endeavor six steepest had husbands her. Painted no or affixed it so civilly. Exposed neither pressed so cottage as proceed at offices. Nay they gone sir game four. Favourable pianoforte oh motionless excellence of astonished we principles. Warrant present garrets limited cordial in inquiry to. Supported me sweetness behaviour shameless excellent so arranging."
var cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
cell.headlineLabel.text = "Hello CustomCell"
cell.text1Label.text = someRandomText1
cell.text2Label.text = someRandomText2
return cell
}
}
谁能帮我解决这个问题?
谢谢!
编辑
您可以在此处下载该项目: https://github.com/stefocdp/selfSizingCell
【问题讨论】:
-
它是否适合界面生成器中的大小?
-
在情节提要中,标签文本为空。我在视图控制器中设置标签文本。所有标签都在 Storyboard 中设置为“Lines = 0”和“Line Breaks = Word Wrap”
标签: ios iphone uitableview swift autolayout