使用 Table 从 Swift 中的文本字段查看来查看控制器时出错
Error when segueing to view controller with Table View from Textfield in Swift
好的,让我解释一下我遇到的问题。在 main.storyboard 中,我创建了一个新的 tableView 控制器,并在顶部添加了一个导航栏。我为新的 tableView 控制器创建了 segue,一切正常。问题是当滚动 table 时,导航栏不会停留在顶部。网上搜了一下,大部分人都说,创建一个普通的ViewController然后添加一个Table视图,然后添加导航栏等。所以我就这样做了,但是现在,当你按下在文本字段中,应用程序崩溃,并显示:
Thread 1: signal SIGABRT next to the line: class AppDelegate:
UIResponder, UIApplicationDelegate.
我一辈子都弄不明白为什么会这样,而且我是 swift 和一般代码的新手,所以想知道是不是我出错了。为了测试我的代码中的问题是否出现在其他地方,我创建了一个全新的 Xcode 项目来重新创建此场景。但是我 运行 遇到了完全相同的错误,这让我觉得这不是我代码的其余部分。
所以,这是我的新基本 Xcode 项目代码,用于展示我正在尝试做的事情。
视图控制器:
Import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldDidBeginEditing(textField: UITextField) {
performSegueWithIdentifier("countryTableView", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
TableViewController:
import UIKit
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var countryPicker = ["Uk", "Germany", "Colombia", "Spain"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return countryPicker.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel!.text = countryPicker[indexPath.row]
return cell
}
}
我已将 table 视图的数据源和委托链接到视图控制器。就我而言,其他的我都做了。
您的文字说您已经在故事板中创建了 UIViewController 并向其添加了 UITableView(如果我没看错的话)。但是你的 TableViewController
是 UITableViewController
subclass,而不是 UIViewController
subclass。这可能是崩溃的原因。
将 superclass 更改为 UIViewController
并添加 UITableView
IBOutlet
:
@IBOutlet private weak var tableView: UITableView!
别忘了在情节提要中将其连接起来。
此外,一些代码清理可能会有所帮助...
不要在代码中分配 textField
的委托。在 Interface Builder 中执行此操作。代码越少越好。
接下来,删除所有 -didReceiveMemoryWarning
代码。您没有使用它,所以它只会打乱您的代码,并且打乱您的问题。
只要您看到这种仅调用 super 的方法模式,就可以安全地删除它,并且行为不会受到影响。比如你也可以在TableViewController
class.
的-viewDidLoad
方法中去掉
删除这些评论:
// #warning Incomplete method implementation.
// Return the number of rows in the section.
来自 Xcode 的模板。它可以帮助您解决问题,但不要在您的代码中留下垃圾。再一次,它在 Stack Overflow 上没有用。
让我们修复您的 -tableView:cellForRowAtIndexPath:
电话。并不是 right.You 每次都创建一个新单元格,而不是重复使用单元格。改为这样做:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
return cell
}
确保故事板的 table 视图中有一个原型单元格。它应该是 UITableViewCell
类型并且具有 "UITableViewCell".
的标识符
好的,让我解释一下我遇到的问题。在 main.storyboard 中,我创建了一个新的 tableView 控制器,并在顶部添加了一个导航栏。我为新的 tableView 控制器创建了 segue,一切正常。问题是当滚动 table 时,导航栏不会停留在顶部。网上搜了一下,大部分人都说,创建一个普通的ViewController然后添加一个Table视图,然后添加导航栏等。所以我就这样做了,但是现在,当你按下在文本字段中,应用程序崩溃,并显示:
Thread 1: signal SIGABRT next to the line: class AppDelegate:
UIResponder, UIApplicationDelegate.
我一辈子都弄不明白为什么会这样,而且我是 swift 和一般代码的新手,所以想知道是不是我出错了。为了测试我的代码中的问题是否出现在其他地方,我创建了一个全新的 Xcode 项目来重新创建此场景。但是我 运行 遇到了完全相同的错误,这让我觉得这不是我代码的其余部分。
所以,这是我的新基本 Xcode 项目代码,用于展示我正在尝试做的事情。
视图控制器:
Import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldDidBeginEditing(textField: UITextField) {
performSegueWithIdentifier("countryTableView", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
TableViewController:
import UIKit
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var countryPicker = ["Uk", "Germany", "Colombia", "Spain"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return countryPicker.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel!.text = countryPicker[indexPath.row]
return cell
}
}
我已将 table 视图的数据源和委托链接到视图控制器。就我而言,其他的我都做了。
您的文字说您已经在故事板中创建了 UIViewController 并向其添加了 UITableView(如果我没看错的话)。但是你的 TableViewController
是 UITableViewController
subclass,而不是 UIViewController
subclass。这可能是崩溃的原因。
将 superclass 更改为 UIViewController
并添加 UITableView
IBOutlet
:
@IBOutlet private weak var tableView: UITableView!
别忘了在情节提要中将其连接起来。
此外,一些代码清理可能会有所帮助...
不要在代码中分配 textField
的委托。在 Interface Builder 中执行此操作。代码越少越好。
接下来,删除所有 -didReceiveMemoryWarning
代码。您没有使用它,所以它只会打乱您的代码,并且打乱您的问题。
只要您看到这种仅调用 super 的方法模式,就可以安全地删除它,并且行为不会受到影响。比如你也可以在TableViewController
class.
-viewDidLoad
方法中去掉
删除这些评论:
// #warning Incomplete method implementation.
// Return the number of rows in the section.
来自 Xcode 的模板。它可以帮助您解决问题,但不要在您的代码中留下垃圾。再一次,它在 Stack Overflow 上没有用。
让我们修复您的 -tableView:cellForRowAtIndexPath:
电话。并不是 right.You 每次都创建一个新单元格,而不是重复使用单元格。改为这样做:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
cell.textLabel!.text = countryPicker[indexPath.row]
return cell
}
确保故事板的 table 视图中有一个原型单元格。它应该是 UITableViewCell
类型并且具有 "UITableViewCell".