0

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!")
    })
  }
}
Shruti Thombre
  • 989
  • 4
  • 11
  • 27
  • 3
    Are you absolutely sure that you linked the IBOutlet? – Mick MacCallum Aug 22 '14 at 19:09
  • Yes, I'm using IB (can I set links without it?). Yes, it's linked to the table view in IB. –  Aug 22 '14 at 19:43
  • As a debugging approach, try printing out the value of self.songsTableView in viewDidLoad and getListOfSongs and in the closure passed for success. Without knowing how SonidoAPIClient works, should you dispatch the call to getListOfSongs() to another thread? – Gary Makin Aug 22 '14 at 23:29
  • Check if your closure is operating on the main thread with `isMainThread`. – Imanou Petit Aug 23 '14 at 09:18
  • @GaryMakin: self.songsTableView is nil in all three places. I lack the knowledge to answer your question =/, but What should I look for? @user1966109: `isMainThread` returned false in the closure. –  Aug 23 '14 at 14:41
  • You have two issues: 1. Your IBOutlet is for any reason not connected to your Storyboard tableView. 2. For the most part, UIKit classes (including UITableView) should be used only from an application’s main thread. So you will have to perform your reloadData in the main thread [See here (Obj-C)](http://stackoverflow.com/questions/7914299/ios-another-thread-needs-to-send-reloaddata-to-the-mainthread). – Imanou Petit Aug 23 '14 at 17:10
  • @user1966109 Thanks for the input, I'll read into it. Is this default behaviour? I don't recall dealing with different threads in my app (I don't even know how to.) –  Aug 23 '14 at 19:04

0 Answers0