blee
blee

Reputation: 295

How do I add multiple objects to a table view cell?

How can I add multiple objects in the same cell?

func getNowPlayingItem() {
    if let nowPlaying = musicPlayer.nowPlayingItem {
        let title = nowPlaying[MPMediaItemPropertyTitle] as? String
        let artist = nowPlaying[MPMediaItemPropertyArtist] as? String
        let album = nowPlaying[MPMediaItemPropertyAlbumTitle] as? String
        println("Song: \(title)")
        println("Artist: \(artist)")
        println("Album: \(album)")
        println("\n")


        self.add = [Play(name: title!)]
        self.table.rowHeight = UITableViewAutomaticDimension
        self.table.estimatedRowHeight = 44.0

    }
}

I tried modifying [Play(name: title!)] to [Play(name: title! + artist! + album!)], but only the title shows up, I'm guessing that only the first object will appear. How can I get all three to show up on three separate lines, but in the same cell?

Upvotes: 0

Views: 264

Answers (2)

Bao Nguyen
Bao Nguyen

Reputation: 137

In tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell set following property for your UITableViewCell.

cell.textLabel?.numberOfLines = 3;
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping

By default, your UITableViewCell only display the first line of text. So, you need to set numberOfLines to 3. Then:

self.add = [Play(name: "\(title!)\n\(artist!)\n\(album !)")]

Notice the newline "\n" character.

Upvotes: 1

James Campbell
James Campbell

Reputation: 3591

You can use string substitution i.e "\(title!) \(artist!) \(album !)" instead.

Upvotes: 1

Related Questions