【发布时间】:2016-09-02 14:56:10
【问题描述】:
我在堆栈上已经有一段时间了,但从来不需要问问题,因为我总是在搜索后找到答案,但现在我真的被困住了。我一直在四处寻找并通过一些试验和错误来寻找答案,但我一直遇到同样的错误。我基本上是在屏幕的下半部分制作一个带有 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
}
}
【问题讨论】:
-
您似乎没有在 UIViewController 代码中注册您的自定义 UITableViewCell 类?
-
尽可能更新屏幕截图,以防止出现此类错误,如果让 mvar = yourvariable {} 或确保您正确绑定 tableview 插座并委托和数据源
-
崩溃在哪一行?
-
@acorc 它必须在使用强制解包的出队。可能只需要: self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "someCustomCell")
-
@Woodstock 假设他没有在情节提要中注册带有该标识符的原型单元(他在顶部有插座)我同意。这就是我停止使用 Storyboard 的原因,太多的晦涩难懂使得追查问题变得困难。
标签: ios swift uitableview parse-platform