user5812721 user5812721 - 6 months ago 11
Swift Question

No data is showing up in my UITableView

My

IBOutlet
for songName is set up correctly, but for some reason

import UIKit

class QueueController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

let testUser = Profile.init(username: "test", followers: [], following: [])
let songOne = Song.init(name: "S1", UWR: testUser.username, genre: "rock", artist: "s1")
var moreSongs = [Song]()
moreSongs = [Song(name: "gah", UWR: testUser.username, genre: "gahh", artist: "GAH")]
Queue.createQueue("2321x", creator: testUser, firstSong: songOne, additionalSongs: moreSongs)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Queue.theQueue!.count
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if let feed = Queue.theQueue {
return feed.count
} else {
return 0
}
}

func songIndex(cellIndex: Int) -> Int {
return tableView.numberOfSections - cellIndex - 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let song = Queue.theQueue![songIndex(indexPath.section)]
let cell = tableView.dequeueReusableCellWithIdentifier("queueCell", forIndexPath: indexPath) as! QueueCell
//cell.textLabel?.text = song.name this doesnt work also
cell.songName.text = "asdad"

return cell
}

}


For my
Queue
class:

import UIKit

class Queue {
let creator:Profile!
let planetID:String!
let beginningSong: Song!
var feed:Array<Song>?
static var theQueue:Array<Song>?

init (id:String!, creator:Profile!, firstSong:Song!) {
self.planetID = id
self.creator = creator
self.beginningSong = firstSong
}

func addSong (song: Song) {
feed?.append(song)
}

static func createQueue (id:String!, creator:Profile!, firstSong:Song!, additionalSongs: [Song]) {
let temp = Queue(id: id, creator: creator, firstSong: firstSong)

for song in additionalSongs {
temp.addSong(song)
}

self.theQueue = temp.feed
}
}


Song
class:

import UIKit

class Song {
let name:String!
let userWhoRequested:String!
let genre: String?
let artist: String?

init (name:String!, UWR:String!, genre:String?, artist:String?) {
self.name = name
self.userWhoRequested = UWR
self.genre = genre
self.artist = artist
}
}


class QueueCell: UITableViewCell {
@IBOutlet weak var songName:UILabel!
@IBOutlet weak var userPicture:UIImageView!
@IBOutlet weak var isCurrent:UIImageView!
}


The table view displays properly but no data shows up in my controller, and I'm not sure why. The reuse cell identifier is also correct. The data in
viewDidLoad()
is also in my app delegate's
func application
.

Thanks!

Answer

Your problem is with the feed inside your CreateQueue method, that was never initialized

And change

var feed:Array<Song>?

to

var feed  = [Song]()
Comments