I basically have a ViewController which shows a few different categories (solved that with a PFQueryTableViewController). Depending on which Cell I tap a segue to the next ViewController is performed, where all objects, which belong to the tapped Category, should appear.
What I did:
I have 2 classes in parse. The class which shows the objects in a Category (the Questions) has a pointer-column to the Category class (filled that pointer-column with the objectIDs from the Category class objects, is that correct?)
In my firstVC: Here I fill my TableView with the categories (works)
class ChooseCategoriesVC: PFQueryTableViewController {
var textOfSelectedCell:String = ""
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Category"
self.textKey = "categoryName"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "Category")
query.orderByAscending("categoryName")
query.whereKey("active", equalTo: true)
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomChooseCategoriesCell!
if Cell == nil {
Cell = CustomChooseCategoriesCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
Cell.chooseCategoryLabel.text = object["categoryName"] as String!
var thumbnail = object["categoryIcon"] as PFFile
var initialThumbnail = UIImage(named: "categoryIcon")
Cell.customChooseCategoryIcon.image = initialThumbnail
Cell.customChooseCategoryIcon.file = thumbnail
Cell.customChooseCategoryIcon.loadInBackground()
return Cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//get text of selected Cell
let indexPath = tableView.indexPathForSelectedRow()!
let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as CustomChooseCategoriesCell!
textOfSelectedCell = selectedCell.chooseCategoryLabel.text!
self.performSegueWithIdentifier("vcToChooseQuestion", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Pass ObjectId of selected Category from ChooseCategoryVC to ChooseQuestionVC
var detailVC : ChooseQuestionVC = segue.destinationViewController as ChooseQuestionVC
detailVC.selectedCategoryText = textOfSelectedCell
}
}
In my secondVC: I need all detailObjects (Questions) to my category
class ChooseQuestionVC: PFQueryTableViewController{
var selectedCategoryText : String!
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// Initialise the PFQueryTable tableview
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.parseClassName = "Question"
self.textKey = "question"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: "Question")
query.whereKey("column", equalTo: self.appDelegate.getParseData(selectedCategoryText))
query.orderByAscending("question")
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var Cell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomChooseQuestionCell!
if Cell == nil {
Cell = CustomChooseQuestionCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
Cell.chooseQuestionLabel.text = object["question"] as String!
return Cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("nextVC", sender: self)
}
}
My function in AppDelegate:
func getParseData(textOfSelectedCell: String) -> PFObject{
var query2 = PFQuery(className:"Category")
query2.whereKey("categoryName", equalTo: textOfSelectedCell)
query2.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
self.selectedObject = object
}
}
}
else {
// Log details of the failure
println("Error: \(error) \(error.userInfo!)")
}
}
println(selectedObject)
return selectedObject!
When running the code like this I always get the error
fatal error: unexpectedly found nil while unwrapping an Optional value
How can I solve that?
column? If not, what iscolumnand what is the pointer called?column.