Leighton
Leighton

Reputation: 6679

Calling multiple CoreData key values for tableview cell display

I have two strings stored with CoreData and I would like to display them both in a tableview cell.

I tried:

cell.textLabel.text = data.valueForKey("firstName, lastName") as? String

with "firstName" being one key and "lastName" being the other, but this won't compile. When I enter multiples like this:

cell.textLabel.text = data.valueForKey("firstName") as? String
cell.textLabel.text = data.valueForKey("lastName") as? String

it only displays the last value (in this case "lastName") in the tableview cell.

Can anyone tell me how to display both the "firstName" and "lastName" on a tableview cell simultaneously?

Upvotes: 0

Views: 180

Answers (1)

fluoresce
fluoresce

Reputation: 452

cell.textLabel.text = data.valueForKey("firstName") as? String
cell.textLabel.text = data.valueForKey("lastName") as? String

Your second assignment is overwriting the first. You need to concatenate the strings together, presumably with a space or a comma between them.

let firstname =  data.valueForKey("firstName") as? String
let lastname =  data.valueForKey("lastName") as? String
let separator = ", "
let label = lastname + separator + firstname
cell.textLabel.text = label

Upvotes: 1

Related Questions