I am trying to setup a very simple table view with data from the network. I do not understand why my UITableView instance is nil. This prevents me from reloading the table's data with the data from the network.
If I use static data var songs: [String] = ["title1", "title2", "title3"]
instead of fetching from the network then everything works fine.
I'm using Xcode 6 Beta 6.
import UIKit
class SongsViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var songsTableView : UITableView!
var songs: [AnyObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.getListOfSongs()
// self.songsTableView is nil here.
// self.songsTableView.dataSource = self
// self.songsTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.songs.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: nil)
cell.textLabel.text = self.songs[indexPath.row] as String
return cell
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
func getListOfSongs () {
SonidoAPIClient.sharedInstance.getSongs(success: {(data: AnyObject) -> Void in
println("Got list of songs: \(data)")
self.songs = data["songs"] as [AnyObject]
self.songsTableView.reloadData() // self.songsTableView is nil here so error is thrown.
}, failure: {
println("Could not get song titles and ids!")
})
}
}