George H George H - 1 year ago 153
iOS Question

Swift UITableView multiline cell

I am trying to add multiple lines to a tableView cell but am not having much success. I have seen related posts suggesting I use :

cell.textLabel?.lineBreakMode = UILineBreakModeWordWrap
cell.textLabel?.numberOfLines = 0

but when I used that I get an error saying: Use of unresolved identifier 'UILineBreakModeWordWrap'. So im not sure why it is throwing that error. Also, how would one go about actually adding a new line? Would it be something like this:

cell.textLabel?.text = "Location Name"
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = UILineBreakModeWordWrap
cell.detailTextLabel?.text = "Locations Address: 123 main street \nNew Line here \nNew Line here"

I am running XCode 6 and developing for iOS 8.
Thank you.

Answer Source

I'm not much of a Swift guy but shouldn't it be:

cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;

or something like that.

UILabel lineBreakMode is of type NSLineBreakMode and the Swift value you want is ByWordWrapping.

The value UILineBreakModeWordWrap is the old Objective-C value that was deprecated in iOS 6.