在 swift 中转到 UITableViewController 时出现黑屏
Black screen when segueing to UITableViewController in swift
所以我有一个 2 屏应用程序,一个主 ViewController 和另一个 ViewController,它在按下按钮时继续运行。这一切都很好,现在仍然如此。
然后我创建了一个新的 ViewController,向其中添加了一个 TableView,嵌入了一个导航控制器并设置了我的 table。我为它创建了一个 segue,这是通过按下 textField 来实现的。一切正常,但偶尔,当它应该转到 TableView VC 时,它只是显示黑屏。没有错误报告,屏幕只是黑色,而且永远不会改变。然而有时一切都很好。它似乎是在我以某种方式更改 table 之后出现的。更烦人的是,当它在模拟器上工作时,如果我然后尝试 运行 构建到我的设备 (iPhone 6) ,当 segue 发生时仍然有黑屏。我真的不知道向您展示什么代码会有用,所以请问您是否想看一些。我会将代码放在 table 视图的 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()
}
}
感谢您的帮助!
对于发生这种情况的原因仍然没有合乎逻辑的原因 - 但是完全清理构建文件夹已经阻止了它的发生。因此,如果您遇到同样的问题,请尝试以下操作:
- 当处于 Xcode 时,转到顶部栏并 select 'Product'
- 当列表出现时,您会看到 'Clean' 选项。
- 按住alt键,选项会变成'Clean build folder'
- 点击'Clean build folder'
所以我有一个 2 屏应用程序,一个主 ViewController 和另一个 ViewController,它在按下按钮时继续运行。这一切都很好,现在仍然如此。
然后我创建了一个新的 ViewController,向其中添加了一个 TableView,嵌入了一个导航控制器并设置了我的 table。我为它创建了一个 segue,这是通过按下 textField 来实现的。一切正常,但偶尔,当它应该转到 TableView VC 时,它只是显示黑屏。没有错误报告,屏幕只是黑色,而且永远不会改变。然而有时一切都很好。它似乎是在我以某种方式更改 table 之后出现的。更烦人的是,当它在模拟器上工作时,如果我然后尝试 运行 构建到我的设备 (iPhone 6) ,当 segue 发生时仍然有黑屏。我真的不知道向您展示什么代码会有用,所以请问您是否想看一些。我会将代码放在 table 视图的 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()
}
}
感谢您的帮助!
对于发生这种情况的原因仍然没有合乎逻辑的原因 - 但是完全清理构建文件夹已经阻止了它的发生。因此,如果您遇到同样的问题,请尝试以下操作:
- 当处于 Xcode 时,转到顶部栏并 select 'Product'
- 当列表出现时,您会看到 'Clean' 选项。
- 按住alt键,选项会变成'Clean build folder'
- 点击'Clean build folder'