加载股票 tableView 时出现致命错误(发现为零)

Xer*_*nox 4 uitableview ios swift

我在tableView里面做了基本的ViewController,在加载时我得到

致命错误:在解开 Optional 值时意外发现 nil

哪个指向tableView.delegate = self(通过指向我的意思是这条线在 Xcode 中以绿色突出显示)。这是完整的代码:import UIKit

import UIKit

class FAQViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, SWRevealViewControllerDelegate {

    @IBOutlet var menuButton: UIBarButtonItem!
    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        if revealViewController() != nil {
            //I have SWRevealController that slides viewController from Left side
        }        
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

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

        let cell = tableView.dequeueReusableCellWithIdentifier("FAQ") as! FAQTableViewCell
        cell.questionLabel.text = "Here goes Question"
        cell.answearLabel.text = "This is answear"

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

rob*_*mac 5

从我从您的示例中可以看出,您似乎正在使用故事板进行设置,但是由于该类是 UIViewController 而不是 UITableViewController,我认为您的连接未正确连接。

我会检查调试器以确保 tableView 不为零并检查故事板以确保连接看起来正常。

您还可以通过右键单击 tableView 来连接 tableViews 数据源并在故事板中委托,然后从数据源或委托对面的圆圈拖动到与故事板场景关联的视图控制器(即层次结构中的第一个图标正下方故事板文件中的场景名称)。

很高兴澄清这是否有意义......