[英]How do I get LineBreakMode to work in UITableViewCell? iOS - Swift
I have a header and a body inside my tableviewcells. 我的tableviewcells中有一个标题和一个主体。 I'm trying to get the body to wrap but I'm having issues.
我正在尝试包裹身体,但遇到了问题。
This is the code I'm using in my view controller 这是我在视图控制器中使用的代码
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("mainCell") as? mainCell {
cell.mainHeader.text = (mainHeader[indexPath.row])
cell.mainBody.text = (mainBody[indexPath.row])
cell.mainBody.numberOfLines = 0
cell.mainBody.lineBreakMode = NSLineBreakMode.ByWordWrapping
return cell
} else {
return mainCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainHeader.count
}
}
I searched and thought that the numberOfLines = 0 and lineBreakMode = NSLineBreakMode would work but it's obviously not. 我搜索并认为numberOfLines = 0和lineBreakMode = NSLineBreakMode可以工作,但显然不行。
Sorry if this is simple, thanks in advance. 对不起,如果这很简单,请先谢谢。
@Daven @戴文
I know this probably won't help, but I don't think I have that or if I do I'm unsure of what it is. 我知道这可能无济于事,但我认为我没有,或者如果我不确定那是什么。 Also updated original code with everything that is in my VC but the vars
还使用VC中的所有内容(除了vars)更新了原始代码
I have this for my custom cell 我有这个用于我的自定义单元
import UIKit
class mainCell: UITableViewCell {
@IBOutlet weak var mainHeader: UILabel!
@IBOutlet weak var mainBody: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configureCell (text:String) {
mainHeader.text = text
mainBody.text = text
}
}
Assuming you want your cell to expand when the text is longer than one line, you want to set the cell height to be automatic using the delegate function: 假设您要在文本长于一行时扩展单元格,则希望使用委托功能将单元格高度设置为自动:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
and set the estimated row height using 并使用设置估计的行高
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return currentInfoSelectorOption.rowHeight + 100
}
Then use autolayout in your cell to set the height. 然后在单元格中使用自动布局来设置高度。 You can do this by setting the top and bottom distance constraints of the cell to the label as a constant value and setting the labels number of lines to 0. This should allow the label to expand and push the cell with it
您可以通过将单元格的顶部和底部距离约束设置为标签的常量值并将标签的行数设置为0来执行此操作。这应允许标签扩展并推动单元格
To make your cell expand as its label grows, you'll need to make sure you've done a few things. 为了使单元随着标签的增长而扩展,您需要确保已完成一些操作。 Set your
tableView
's estimatedRowHeight
to a value, like 设置您
tableView
的estimatedRowHeight
的值,如
tableView.estimatedRowHeight = 200.0;
tableView.rowHeight = UITableViewAutomaticDimension
or via this method: 或通过这种方法:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 200.00
}
You also have the option of using this method, but it's not necessary if all your cells are going to vary in height: 您还可以选择使用此方法,但是如果所有单元格的高度都将发生变化,则没有必要:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You may also find this tutorial helpful (this is what I used :)). 您可能还会发现本教程很有用(这是我使用的:))。 It shows what your autolayout constraints should look like.
它显示了自动布局约束的外观。 More information about self-sizing cells is also here .
有关自定尺寸电池的更多信息也在此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.