[英]Unable to display new lines for array data in UILabel
I have an array.我有一个数组。 In that I have data like
因为我有这样的数据
"multi": ["New York","Los Angeles", "California", "Huston"]
And this above array I am trying to display in UILabel
.上面这个数组我试图在
UILabel
显示。
cell.dynamicLbl.text = array.joined(separator: "\n")
even tried with following甚至尝试以下
cell.dynamicLbl.text = array.joined(separator: ",")
but, its showing only first index of an array.但是,它只显示数组的第一个索引。 Even, I added label properties to show multiple lines.
甚至,我添加了标签属性来显示多行。
cell.dynamicLbl.lineBreakMode = .byWordWrapping
cell.dynamicLbl.numberOfLines = 0
My total array is if I print in console我的总数组是如果我在控制台中打印
▿ 4 elements
- 0 : "New York"
- 1 : "Los Angeles"
- 2 : "California"
- 3 : "Huston"
And I already set my label with constraints.我已经用约束设置了我的标签。 PFA.
煤灰。
And took height constant for constraint and calculating the height of the text and assigning that float value to constraint constant.并采用高度常量进行约束并计算文本的高度并将该浮点值分配给约束常量。
@IBOutlet weak var optionsLblHeightConstraint: NSLayoutConstraint!
Still showing first data of array in label.仍然在标签中显示数组的第一个数据。
Any suggestions?有什么建议?
Out put输出
Check your cell.label.numberOfLines
is set to 0
and lineBreakimgMode
to .wordWrap
.检查您的
cell.label.numberOfLines
设置为0
并且lineBreakimgMode
设置为.wordWrap
。
And cell constraints for top and bottom are set to contentView and heightForRow tableview func returns tableview.automaticDimension
顶部和底部的单元格约束设置为 contentView 和 heightForRow tableview func 返回
tableview.automaticDimension
Instead of setting the Height
for your label in the cell, try to set top, bottom, leading, trailing anchors.不要在单元格中为标签设置
Height
,而是尝试设置顶部、底部、前导和尾随锚点。
You need to make sure that the label has auto layout set and it is anchored with topAnchor
and bottomAnchor
inside the cell.您需要确保标签设置了自动布局,并且在单元格内使用
topAnchor
和bottomAnchor
锚定。 Afterwards set the following code inside ViewDidLoad
然后在
ViewDidLoad
里面设置如下代码
//this is to automatically manage the height of the label.
tableView.rowHeight = UITableViewAutomaticDimension
//give a approximate height for cell below
tableView.estimatedRowHeight = 300
Don't set the height constraint of your UILabel
.不要设置
UILabel
的高度约束。 Just add trailing
, leading
, top
and bottom
constraints.只需添加
trailing
、 leading
、 top
和bottom
约束。
No need to calculate height.无需计算高度。 Set
lineNumber
of your UILabel
as 0 .将
UILabel
lineNumber
设置为0 。
Inside viewDidLoad
set the below properties of your UITableView
.在
viewDidLoad
设置UITableView
的以下属性。
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
Please check there should not be any height calculations anywhere and heightForRowAtIndexPath
should not be written for this auto-resizing cells.请检查不应在任何地方进行任何高度计算,并且不应为此自动调整大小的单元格编写
heightForRowAtIndexPath
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.