2

i have been busting my brain trying to figure out how this works, but i can't seem to get it. i have tried using other tutorials, but with the many beta releases, everything keeps changing. i am fairly new to IOS development, so i'm kind of struggling.

in storyboard i have UITableView, which contains a cell with the identifier "myCell".

here's what i have so far. when i run the IOS simulator, nothing is presented on the table view.

any suggestions on how to fix this?

class ViewController: UITableViewController {
  override func viewDidLoad() {
      super.viewDidLoad()
  }
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     var cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
     cell.textLabel?.text = "Cell #: \(indexPath.row)" // display the row number
     return cell
  }
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 10; // testing out with 10 cells
  }
}
2
  • What version of Xcode are you running? I can't get this to compile as is. Commented Sep 4, 2014 at 21:10
  • The new beta released. Xcode 6 Beta 7. Commented Sep 4, 2014 at 21:14

1 Answer 1

4

Add the function

optional func numberOfSectionsInTableView(tableView: UITableView) -> Int

and return the number of sections you want.

You should make sure in the storyboard your UITableViewController has the class ViewController like so:

enter image description here

and that ViewController is both the delegate and datasource of the UITableViewController like so (Referencing Outlets):

enter image description here

You should also check that your UITableViewController is set to initialViewController if you don't see any lines at all (check the one at the bottom).

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks a lot, man! I changed the custom class from "UITableViewController" to "ViewController" and it worked. Could you explain why?
I'm no expert but here's my explanation: iOS will load the UITableViewController with the class UITableViewController, which will just display an empty UITableViewController. ViewController is a subclass of UITableViewController, essentially adding functionality to the UITableViewController class. In the storyboard, we need to make it clear the the UITableViewController is of the class ViewController so it gets all the extra functionality you added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.