Listing contents using UITableView - ios

I am a newbie to iOS programming and I am simply trying to set up a TableView which would populate array contents.
I keep running into the following error.
uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.'
I looked into a couple suggested solutions already but none of them work for my case.
Some suggested that I make my ViewController to be a subclass of UIViewController but I get the following error from doing so.
Another suggested I use UIViewController instead of UITableViewController in my custom class but I get the following error doing so.
My custom class code
//
// AddQuestionTableViewController.swift
// Quiz Add Question
//
// Created by Emil Shirima on 7/29/15.
// Copyright (c) 2015 Emil Shirima. All rights reserved.
//
import UIKit
class AddQuestionTableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var sample = [String]()
override func viewDidLoad() {
super.viewDidLoad()
sample = ["apple","pear","banana","raspberry"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return sample.count
}
// indexPath has info about which section and which row
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("questionCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
let data = sample[indexPath.row]
cell.textLabel?.text = sample[indexPath.row]
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
My ViewController code
//
// ViewController.swift
// Quiz Add Question
//
// Created by Emil Shirima on 7/29/15.
// Copyright (c) 2015 Emil Shirima. All rights reserved.
//
import UIKit
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
My current storyboard with hierarchy is found here: //i.stack.imgur.com/2lEVe.png.
Sorry, not enough reputation points to post extra link.
I do have a general idea as to why this is happening but if anyone would explain to me exactly why and how to solve it, I'll sincerely appreciate it.

Please check ViewController named "AddQuestionTableViewController" on Storyboard.
I think you named "AddQuestionTableViewController" to wrong ViewController (not UITableViewController)

Some common causes for this have to do with Storyboard connections and the Table View Controller file. First, click on the Table View itself in Storyboard and check the Outlets.
If they are disconnected, drag from the circles in delegate and data source to the little table view controller icon at the top of the view controller.
Also, make sure your class is hooked up correctly. Go the ID page in storyboard for your Table View Controller (NOT the Table itself) and check the class.

The error had told you the details: [UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.
You should check your custom UITableView.
Your problem maybe:
Your did't set custom UITableView Identity to "BYZ-38-t0r-view-8bC-Xf-vdC" in you xib file. If so, go to xxx.xib->show identity inspector->Identity set Restoration Id to "BYZ-38-t0r-view-8bC-Xf-vdC".
You've set a xib file's Identity to "BYZ-38-t0r-view-8bC-Xf-vdC", but it's not a UITableView, it's a UIView or something. If so, set the identity to a custom UITableView

Related

UITableView is not displaying data?

I took an UIViewController. Then brought a TableView into it. And then a TableViewCell in the tableview. I assigned identifier and custom table view cell class with that prototype cell. Also created necessary outlets. Then this is my parent view controller:
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var theTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// let cell = MyTableViewCell()
// cell.optionText = UILabel()
// cell.optionText?.text = "testing..."
// theTableView.addSubview(cell)
theTableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTableViewCell
cell.optionText.text = "hello..."
return cell
}
}
But my table is empty. What am I missing?
You have to add the following line in your viewDidLoad:
theTableView.dataSource = self
When you do that, you provide dataSource to your Tableview from your ViewController and then your TableView starts showing data.
The methods cellForRowAtIndexPath and rowsForSection are both Datasource methods. Since you haven't set your dataSource that's why your TableView fails to load the provided data.
Also if you haven't added the TableView via Storyboard/Xib, you also have to add it as a subview of your view.

getting a table view in viewController

I have got a viewController in storyboard with following file
import UIKit
class jokesviewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
and another controller in which i use this controller by making its object , (well this a JSQmessagesController Class in which i make object of jokesviewController class)
and the code i use is
var jokeviewController :jokesviewController? //at class begnning
and somewhere in some function
jokeviewController = jokesviewController()
self.view.addSubview(jokeviewController!.view)
jokeviewController!.view.backgroundColor = UIColor.grayColor()
my question is i get to use jokeviewController!.view giving me a view to use but when i try to get jokeviewController!.tableview it says i got a nil value .
I have tried both ways putting tableView inside a view and tableview without container view in the controller .
Any guesses
First of all, always use upper case for controllers( or classes ).
Secondly you instantiate view controller as is, but IB outlets and other stuff lives in storyboard. So, instead of
jokeviewController = jokesviewController()
use
jokeviewController = UIStoryboard(name: "yourStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("giveYourViewControllerIdentifier") as! jokesviewController
and you will have everything set up.
For giving identifier for your view controller in storyboard, select your view controller in storyboard, select third tab and set storyboard ID to whatever you want.

Nothing appearing in UITableView cell

I am working on an app that needs to display five different images inside of five different tableview cells. This is the storyboard that I have for the cell.
This is the code I have inside of the tableviewcontroller.
class MasterViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
var returnTable = 1
var icons: [String] = ["settings", "calendar", "classes", "envelope", "grades"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return icons.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! HomeScreenCells
var image : UIImage = UIImage(named: icons[indexPath.row])!
cell.icon.image = image
return cell
}
I also have a custom class for the tableviewcells. Also, I have the correct reuse identifier for the cell. Still, the image does not appear. Not only does the image not appear, nothing appears. When I change the background, the background does not change in the simulator. This is all I get in the simulator.
I have the classes linked up correctly for the split view controller. I have no idea why nothing is appearing in the table. Any help would be appreciated.
I think your problem may be that you have the number of sections set to 0. Either set it to 1, or leave out the block of code completely.
As stated above, return 1 in number of sections (or the number of cells you need). And if your custom cell class has a .xib, you also need to register it, sometimes after you init the tableview
tableView.registerNib( UINib(nibName: "HomeScreenCells", bundle: nil), forCellReuseIdentifier: "Cell")

Unable to display data in the second view controller using segue [Swift]

I need to pass data from one view controller to another view controller. I used segue (detail) and define a model class named as "Photo".
TableViewController looks like the following:
var photos = [Photo]() //strongly typed swift array
override func viewDidLoad() {
super.viewDidLoad()
var newPhoto = Photo(name:"cat ", fileName:"cat", notes:"cat_file")
photos.append(newPhoto)
var newPhoto2 = Photo(name:"r2 ", fileName:"r2", notes:"r2")
photos.append(newPhoto2)
}
And the other view controller (DetailViewController) looks like the following:
import UIKit
class PhotoDiplayViewController: UIViewController {
var currentPhoto: Photo?
#IBOutlet weak var currentImage: UIImageView!
#IBOutlet weak var currentLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var image = UIImage(named: currentPhoto!.fileName)
self.currentImage.image = image
self.currentLabel.text = currentPhoto?.name
println(currentPhoto!.name + currentPhoto!.fileName + currentPhoto!.notes)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I am running the program, the table view is loading fine and if i click on any cell it is going to the detail view controller. but noting is there in the detail view controller. And I used println() to check and the output is coming in the debugger like the following:
cat cat cat_file
To pass data, I used the following segue code block:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
var secondScene = segue.destinationViewController as! PhotoDiplayViewController
if let indexPath = self.tableView.indexPathForSelectedRow(){
let selectedPhoto = photos[indexPath.row]
secondScene.currentPhoto = selectedPhoto
}
}
But still no luck! Tried to figure out where I am missing? Can anybody tell me where I am lagging?
UPDATE: complete detail view controller class code
UPDATE: Full detail of My table view code
import UIKit
class PhotoTableViewController: UITableViewController {
var photos = [Photo]() //strongly typed swift array
override func viewDidLoad() {
super.viewDidLoad()
var newPhoto = Photo(name:"cat ", fileName:"cat", notes:"cat_file")
photos.append(newPhoto)
var newPhoto2 = Photo(name:"r2 ", fileName:"face.jpg", notes:"r2")
photos.append(newPhoto2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return photos.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("photoCell", forIndexPath: indexPath) as! UITableViewCell
var currentPhoto = photos[indexPath.row]
cell.textLabel?.text = currentPhoto.name
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
var secondScene = segue.destinationViewController as! PhotoDiplayViewController
if let indexPath = self.tableView.indexPathForSelectedRow(){
let selectedPhoto = photos[indexPath.row]
secondScene.currentPhoto = selectedPhoto
}
}
}
Don't use a segue. Use this, its easier.
Follow these steps...
1: Create a Separate file called Manager.swift and place this code in it...
//manager.swift
import Foundation
struct Manager {
static var dataToPass = String()
}
2: Clean your project by pressing Shift+Command+K.
3: In the first view controller set the dataToPass to the data you want to pass...
Manager.dataToPass = self.dataToPass
4: In the second view controller retrieve the data and set the content to the dataToPass...
self.dataToReceive = Manager.dataToPass
5: Your Finished!!
The code which I presented is working fully after deleting all the image view and label from the storyboard and remapping. But I am wondering what was the problem. However, I want to share one screen shot:
In the screen shot, you will see 3 components: 2 labels and 1 image view. One 1 label's text is dark black colored but the for the other 2 it's not alike them. All of them are properly configured.
Still I don't know why this happens? I am not sure .... is it possible to add some hidden components on the top of storyboard??? or is it a bug of Xcode????
However, if you have similar experience please share. My aim is not only solve the problem but also to understand the cause of the problem.
:)

add identifier to segue

I have created a segue from a button, but it has given me the following error. I am using Swift and Xcode 6.
/Users/dave_cooljamdesign/Desktop/XcodeDev/findaphysio2/FindaPhysio/FindaPhysio/Base.lproj/Main.storyboard: Segues initiated directly from view controllers must have an identifier
How do I give them an identifier when I'm draging from table view to the detail view?
Below is some code to show what I am doing in the view controller
import UIKit
import MapKit
class ClinicsTableViewController:
UITableViewController {
var clinic = [Clinics]()
#IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.clinic = [Clinics(name:"Chocolate")]
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return self.clinic.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
let cell = self.tableView.dequeueReusableCellWithIdentifier("findaphysioCell", forIndexPath: indexPath) as UITableViewCell
// Get the corresponding candy from our candies array
let _clinic = self.clinic[indexPath.row]
// Configure the cell
cell.textLabel!.text = _clinic.name
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
}
Find the segue on the storyboard (the line between view controllers with a circle in the middle) and click it.
Then open the attributes inspector in the utilities pane and give it an identifier.
As gabbler mentioned, the shortcut command + option + 4 will immediately open the attributes inspector.
In your storyboard, select the segue between the two view controllers. Go to the attributes inspector on the right and add an identifier name.
In your code do something like this:
self.performSegueWithIdentifier("setupView", sender: self)
Obviously change 'setupView' with whatever you set for the segue identifier.

Resources