加载tableView时解开可选值时意外发现nil [英] Unexpectedly found nil while unwrapping an optional value when loading tableView

查看:73
本文介绍了加载tableView时解开可选值时意外发现nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间了,但是我从来不需要问任何问题,因为经过一番搜索,我总是能找到答案,但是现在我还是坚持到底.我一直在寻找答案,并经历了一些试验和错误,但我一直遇到相同的错误.我基本上是在屏幕的下半部分创建一个带有tableView的配置文件页面.上半部分正在加载当前用户的信息.到视图控制器和单元格视图控制器的所有连接似乎都很好.但是,加载致命错误时,表视图将不显示任何数据并且崩溃:

I've been on stack for a while now but never needed to ask a question as I've always found the answers after some searching, but now I'm stuck for real. I've been searching around and going through some trial and error for an answer and I keeping getting the same error. I'm basically making a profile page with a tableView on the bottom half of the screen. The top half is loading fine filling in the current user's information. All connections to the view controller and cell view controller seem good. The table view, however, will appear with no data and crash while loading with the fatal error:

在展开一个可选值时意外发现nil.

unexpectedly found nil while unwrapping an optional value.

我还认为根本不会调用cellForRowAtIndexPath,因为测试"没有打印到日志中.

I also believe the cellForRowAtIndexPath is not being called at all because "test" is not printing to the logs.

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

I'm using the latest versions of Swift and Parse.

我是新手,所以我继续在这里发布我的整个代码,对您的任何帮助都表示赞赏.

I'm relatively new to swift so I'll go ahead and post my entire code here and any help at all is appreciated.

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
}

}

推荐答案

我让它坐了几天,然后我回来意识到自己犯了一个非常愚蠢的错误.我现在与大约15个视图控制器一起工作,并且意识到我有一个与我上面发布的同名视图控制器的副本.现在,我明白了为什么您说使用情节提要板很棘手.虽然我不需要它,但我很感谢您的帮助,可以说我学到了一些东西.

I let it sit for a few days and I came back to realize a very dumb mistake I made. I working with around 15 view controllers right now and realized I had a duplicate of the one I posted above with the same name. I now understand why you say working with storyboards is very sticky. Though, I did not need it, I appreciate the help and I can say I learned a few things.

这篇关于加载tableView时解开可选值时意外发现nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆