如何设置自定尺寸单元 iOS8
Posted
技术标签:
【中文标题】如何设置自定尺寸单元 iOS8【英文标题】:How to set up self sizing cells iOS8 【发布时间】: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” 【参考方案1】:我通过将这行代码添加到 cellForRowAtIndexPath 函数来修复它
cell.layoutIfNeeded()
我的 ViewController 现在看起来像这样:
import UIKit
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad()
super.viewDidLoad()
self.tableView.estimatedRowHeight = 50
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
cell.layoutIfNeeded()
return cell
【讨论】:
太好了,我正在给您写一个与 ios7 兼容的答案,但看到它不值得发布。我不知道 ios8 中单元格的自动布局改进,所以现在我们不需要heightForRowAtIndexPath
恶作剧。有一个有用的比较 iOS7 v iOS8 tableview cell resizing here
+还有这个 amazingly detailed SO answer 详细介绍了 iOS7 和 iOS8 模式
还是谢谢你。使用此解决方案,我在控制台中遇到约束错误,但 UI 看起来很完美。希望我能尽快摆脱这些。我一定会查看链接的 SO 答案。谢谢!【参考方案2】:
我相信您的问题来自估计的RowHeight。您必须将其设置为更接近现实。
在您使用当前约束的情况下,只有标签之间的空间为 48。我也遇到了这个问题,如果不将estimatedRowHeight 设置为合理的数字,会使表格视图不再计算任何好的东西。
尝试将其设置为 110(每个标签 3 x 20 + 它们之间的 48)。
【讨论】:
谢谢。我试过了,但没有用。我还在一个测试用例中通过 heightForRowAtIndexPath 设置了高度。并且行高大约是 600。所以我尝试将estimateRowHeight 设置为 600。但它仍然无法正确显示。 我添加了一个 GitHub 链接,您可以在其中下载项目以上是关于如何设置自定尺寸单元 iOS8的主要内容,如果未能解决你的问题,请参考以下文章
UIImage 中的自定尺寸 UITableViewCell 拉伸