简体   繁体   English

无法在 UILabel 中为数组数据显示新行

[英]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.您需要确保标签设置了自动布局,并且在单元格内使用topAnchorbottomAnchor锚定。 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.只需添加trailingleadingtopbottom约束。

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

Finally I found the answer, Its the issue with contraint relation.最后我找到了答案,这是约束关系的问题。 Earlier it was like "Equal" relation.早些时候它就像“平等”关系。 After changing to "Grater Than or Equal" fixed the issue.更改为“大于或等于”后修复了该问题。

Hope this will help someone in future.希望这会在将来对某人有所帮助。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM