简体   繁体   中英

Black screen when segueing to UITableViewController in swift

So I had a 2 screen app all working, a main ViewController, and another ViewController, which segues on a button press. This all works fine, and still does.

I then created a new ViewController, added a TableView to it, embedded a Navigation controller and set up my table. I created a segue to it, which happens by pressing in a textField. It all works fine, but occasionally, when it's supposed to segue to the TableView VC, it just shows a black screen. No errors are reported, the screen is just black, and it never changes. Sometimes however it's all fine. It seems to appear after I've changed the table in some way. Whats more annoying is, when it does work on the simulator, if i then try to run the build to my device (iPhone 6) , there is still a black screen when the segue happens. I don't really know what code will be useful to show you, so please ask if you want to see some. I'll put the code thats on the tableView's VC.

import UIKit

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate {

@IBOutlet private weak var tableView: UITableView!

var countryPicker = ["Colombia", "England", "France", "Germany", "Brazil", "Austria", "Spain", "Australia", "USA", "Berlin", "Thailand", "Northern Ireland", "New Zealand", "Hawaii", "Switzerland", "Sweeden", "Finland", "Turkey", "Russia", "Netherlands", "Japan"]


override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.rowHeight = 50
    self.tableView.backgroundView = UIImageView(image: UIImage(named: "TblBackground"))

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return countryPicker.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

    if (indexPath.row % 2 == 0) {

        cell.backgroundColor = UIColor.clearColor()

    } else {

        cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)

        cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)

    }

    var imageView: UIImage! = UIImage(named: "Australia")
    cell.imageView?.image = imageView
    cell.imageView?.highlightedImage = UIImage(named: "Australia1")

    cell.textLabel?.text = countryPicker[indexPath.row]

    cell.textLabel?.textColor = UIColor.whiteColor()

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
    cell?.textLabel!.textColor = UIColor.blueColor()

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell!.accessoryType = UITableViewCellAccessoryType.None
    cell?.textLabel!.textColor = UIColor.whiteColor()

}

} 

thanks for any help!

There is still no logical reason as to why this happened - but doing a full clean of the build folders has stopped it happening. So if you're having the same issue, try this:

  1. When in Xcode, go to the top bar and select 'Product'
  2. When the list appears, you'll see the 'Clean' option.
  3. Hold down the alt key and the option will change to 'Clean build folder'
  4. Click 'Clean build folder'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM