我的UITableView中没有数据显示

时间:2016-05-30 17:00:29

标签: ios xcode swift uitableview

我的IBOutlet为songName设置正确,但出于某种原因

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
    }

}

对于我的Queue班级:

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上课:

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!
}

表格视图显示正常但我的控制器中没有数据显示,我不知道为什么。重用小区标识符也是正确的。 viewDidLoad()中的数据也在我的应用代表的func application中。

谢谢!

1 个答案:

答案 0 :(得分:2)

您的问题在于CreateQueue方法中的Feed,它从未初始化

并改变

var feed:Array<Song>?

var feed  = [Song]()