繁体   English   中英

在Swift中从文本字段中选择使用表视图的视图控制器时出现错误

[英]Error when segueing to view controller with Table View from Textfield in Swift

好吧,让我解释一下我遇到的问题。 在main.storyboard中,我创建了一个新的tableView控制器,并在顶部添加了一个导航栏。 我为新的tableView控制器创建了segue,一切正常。 问题是在滚动表时导航栏没有停留在顶部。 搜索互联网后,大多数人说,只需创建一个普通的ViewController并向其中添加一个Table View,然后添加导航栏等。所以我做到了,但是现在,当您在文本字段中按一下时,该应用程序就会崩溃,并显示:

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

我已将数据源和表View的委托链接到View Controller。 就我而言,我已经做了其他所有事情。

您的文字说您已经在情节提要中创建了UIViewController并向其中添加了UITableView(如果我正确阅读的话)。 但是您的TableViewControllerUITableViewController子类,而不是UIViewController子类。 这可能是导致崩溃的原因。

UIViewController类更改为UIViewController并添加UITableView IBOutlet

@IBOutlet private weak var tableView: UITableView!

不要忘记将其连接到情节提要中。

另外,一些代码清理可能会有所帮助...


不要在代码中分配textField的委托。 在Interface Builder中执行该操作。 代码越少越好。


接下来,删除所有-didReceiveMemoryWarning代码。 您没有使用它,因此它只是使您的代码混乱,并且使您的问题在这里混乱。

每当您看到这种仅调用super的方法模式时,就可以安全地将其删除,并且该行为不会受到影响。 例如,您也可以在TableViewController类的-viewDidLoad方法中将其删除。


删除这些评论:

// #warning Incomplete method implementation.
// Return the number of rows in the section.

这来自Xcode的模板。 这是为了帮助您,但请不要在代码中留下垃圾。 再说一次,它在堆栈溢出中没有用。


让我们修复-tableView:cellForRowAtIndexPath:调用。 这是不对的。您每次都在创建一个新单元,而不是重复使用单元。 改为这样做:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell") as! UITableViewCell
    cell.textLabel!.text = countryPicker[indexPath.row]
    return cell
}

确保情节提要的表格视图中有一个原型单元。 它应该是UITableViewCell类型,并且具有“ UITableViewCell”标识符。


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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