加载 tableView 时解包可选值时意外发现 nil

bop*_*ppa 1 uitableview ios parse-platform swift

我已经在堆栈上有一段时间了,但从来不需要问一个问题,因为我总是在一些搜索后找到答案,但现在我真的被困住了。我一直在四处寻找并通过一些试验和错误来寻找答案,但我不断收到同样的错误。我基本上是在屏幕的下半部分制作一个带有 tableView 的个人资料页面。上半部分正在加载精细填充当前用户的信息。与视图控制器和单元格视图控制器的所有连接看起来都很好。但是,表视图将显示为没有数据并在加载致命错误时崩溃:

解包可选值时意外发现 nil。

我也相信cellForRowAtIndexPath根本没有被调用,因为“测试”没有打印到日志中。

我正在使用最新版本的 Swift 和 Parse。

我对 swift 比较陌生,所以我会继续在这里发布我的整个代码,并感谢任何帮助。

import UIKit
import Parse
import ParseUI

class profileViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet var tableView: UITableView!
@IBOutlet var profilePic: UIImageView!
@IBOutlet var userName: UILabel!
@IBOutlet var userBio: UILabel!
var image: PFFile!
var username = String()
var userbio = String()
var content = [String]()


@IBAction func logout(sender: AnyObject) {
    PFUser.logOut()
    let Login = storyboard?.instantiateViewControllerWithIdentifier("ViewController")
    self.presentViewController(Login!, animated: true, completion: nil)

}

override func viewDidLoad() {
    super.viewDidLoad()


    profilePic.layer.borderWidth = 1
    profilePic.layer.masksToBounds = false
    profilePic.layer.borderColor = UIColor.blackColor().CGColor
    profilePic.layer.cornerRadius = profilePic.frame.height/2
    profilePic.clipsToBounds = true


    tableView.delegate = self
    tableView.dataSource = self


    self.tableView.rowHeight = 80


    self.hideKeyboardWhenTappedAround()

    if let nameQuery = PFUser.currentUser()!["name"] as? String {
        username = nameQuery
    }

    if PFUser.currentUser()!["bio"] != nil {
    if let bioQuery = PFUser.currentUser()!["bio"] as? String {
        userbio = bioQuery
    }
    }

    if PFUser.currentUser()!["icon"] != nil {
    if let iconQuery = PFUser.currentUser()!["icon"] as? PFFile {
        image = iconQuery
    }
    }



    self.userName.text = username
    self.userBio.text = userbio

    if image != nil {
    self.image.getDataInBackgroundWithBlock { (data, error) -> Void in


        if let downIcon = UIImage(data: data!) {


            self.profilePic.image = downIcon

        }
        }

    }


    // Do any additional setup after loading the view.

    var postsQuery = PFQuery(className: "Posts")

    postsQuery.whereKey("username", equalTo: username)

    postsQuery.findObjectsInBackgroundWithBlock( { (posts, error) -> Void in

        if error == nil {

            if let objects = posts {
                self.content.removeAll(keepCapacity: true)

                for object in objects {
                    if object["postText"] != nil {
                        self.content.append(object["postText"] as! String)
                    }
                    self.tableView.reloadData()
                }
            }
        }

    })

}




override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    print(content.count)
    return content.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let profCell = self.tableView.dequeueReusableCellWithIdentifier("profCell", forIndexPath: indexPath) as! profTableViewCell

    print("test")

    profCell.userPic.layer.borderWidth = 1
    profCell.userPic.layer.masksToBounds = false
    profCell.userPic.layer.borderColor = UIColor.blackColor().CGColor
    profCell.userPic.layer.cornerRadius = profCell.userPic.frame.height/2
    profCell.userPic.clipsToBounds = true


    profCell.userPic.image = self.profilePic.image
    profCell.name.text = self.username




    profCell.content.text = content[indexPath.row]



    return profCell
}

}
Run Code Online (Sandbox Code Playgroud)

bop*_*ppa 6

我让它坐了几天,然后我回来意识到我犯了一个非常愚蠢的错误。我现在使用了大约 15 个视图控制器,并意识到我有一个我在上面发布的同名的副本。我现在明白为什么你说使用故事板很粘。虽然,我不需要它,但我很感激帮助,我可以说我学到了一些东西。