简体   繁体   中英

Multiple lines in detail text label of table view cell

I am trying to have multiple lines in a cell's detailTextLabel but with discrete information on each line. In order to get multiple lines in the detailTextLabel I would use

cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

But I am looking to have specific text on each line. I know swift doesn't have a line break so I believe it would have to look like this:

 cell.detailTextLabel!.text = "Line 1" +
                        "Line 2" +
                        "Line 3" +
                        "Line 4" +
                        "Line 5"

But this returns Line1Line2Line3Line4Line5 in the detailsTextLabel.

I can successfully have multiple lines by putting in a super long string and have it wrap down to multiple lines.

Is there a simple way to accomplish this? Or just not possible?

感谢 Shoaib,您所要做的就是在字符串中实现 "\\n"

cell.detailTextLabel!.text = "Line 1" + "\n" + "Line 2" + "\n" + "Line 3" + "\n" + "Line 4" + "\n" + "Line 5"

You can use the multiline string format like:

cell.detailTextLabel!.text = """
Line 1
Line 2
Line 3
Line 4
Line 5
"""

Trick

If you want to add something like between multiple items (Like @Tamarisk answer) you can get help from an array like:

[
    "Line 1",
    "Line 2",
    "Line 3",
    "Line 4",
    "Line 5",
].joined(separator: "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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