user3746428
user3746428

Reputation: 11175

Table View displays blank screen

I am having issues with with a TableViewController displaying a blank screen. I have just changed my 'Personalisation' screen from a ViewController to a TableViewController however when the personalisation cell is tapped and the app segues to the personalisation screen, all that is displayed is a blank screen.

Here is an image of how it should look as well as how it used to look on the right:

enter image description here

This is how it looks when run:

enter image description here

The content is set to Static and style set to Grouped.

This is the only code I have at the moment as I haven't finished transitioning the code from the old version to the new version since I just ran into this issue...

import UIKit

class PersonalisationTableViewController: UITableViewController, UIPickerViewDelegate {

@IBOutlet weak var changeButton: UIButton!
@IBOutlet weak var userNameTextField: UITextField!

let coloursArray = ["Default (Blue)", "Green", "Orange", "Purple", "Red", "Turquoise"]

@IBAction func userNameChange(sender : AnyObject) {
    //Closes keyboard when user touches enter button

    userNameTextField.resignFirstResponder()
    globalUserName = userNameTextField.text
    globalUserName = globalUserName.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    userNameTextField.placeholder = globalUserName
    userNameTextField.text = ""
}

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

/*
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell

    // Configure the cell...

    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.
}
*/

}

I've not worked with table views much so I'm sure the issue is obvious, but I can't figure it out.

Upvotes: 0

Views: 3182

Answers (2)

Apeksha Sahu
Apeksha Sahu

Reputation: 57

This error has a very simple fix:

Select the ViewController Scene. Now, select the inspector panel from the sidebar options. Check the “Is Initial View Controller” attribute option, save the project and build the project again.

Upvotes: 2

Shan
Shan

Reputation: 3057

It is most likely because your numberOfSectionsInTableView and numberOfRowsInSection methods are returning 0. You have your UITableView set up in your storyboard but the two methods override that. When you have static cells in your UITableView, you should not override these two methods. They were probably left over from when you created the class as a subclass of UITableViewController.

Here is what is basically happening: Your UITableViewController hooked up to your PersonalisationTableViewController file. When your PersonalisationTableViewController sets up the sections and rows for your tableView, it is setting the sections to 0 and the rows for each section to 0.

I just tried putting the two methods into one of my apps that has static cells and I can confirm that it appears blank.

If you were programmatically creating your views, you could have done the following:

   override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    // Return the number of sections.
    return 2
}

   override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return 1

     // Or this if you had a different number rows for each section:

        if section == 0 {
            return //someInt
        } else if section == 1 {
            return //someOtherInt
        } else {
            //......
        }

}

EDIT: Also, I believe this is a bug with Xcode but you must override:

    override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    if indexPath.section == 0 {
        return someHeight
    } else if indexPath.section == 1 {
        return someOtherHeight
    } else {
        // ....
    }
}

Upvotes: 3

Related Questions