user8105388
user8105388

Reputation:

how to place 2 coredata attributes in a single tableview cell. (swift4)

My code right now calls one core data attribute "lorde" and places it on a tableview cell. I want the cell to display both "lorde" and other attribute "num". I want both attributes to be printed on
cell.textLabel?.text.

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemsName[indexPath.row]

    let cell = tazbleView.dequeueReusableCell(withIdentifier: "cell", for : indexPath)

    cell.textLabel?.text = title.value(forKey: "lorde") as? String


    return cell

}

Upvotes: 0

Views: 63

Answers (2)

Andrey
Andrey

Reputation: 1206

I prefer this way It looks more nice and not stop of using when just one attribute exists

let attr1 = title.value(forKey: "lorde") as? String
let attr2 = title.value(forKey: "num") as? String 
cell.textLabel?.text = [attr2, attr1].flatMap { $0 }.reduce("", +)

Upvotes: 1

koen
koen

Reputation: 5729

You can concatenate the two strings with the + operator.

guard let lorde = title.value(forKey: "lorde"),
      let num = title.value(forKey: "num")
else { fatalError("cannot unwrap title keys") }

cell.textLabel?.text = lorde + num

Upvotes: 0

Related Questions