I am trying to set up a table view from a Parse database. Each cell should contain several pieces of data about one object (coded in Parse as what and where
). When tapped, it should transition to a new page showing all available data about that object (including username, and when).
Here's my code for the table view:
import UIKit
import Parse
import ParseUI
class TableView: PFQueryTableViewController {
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 = "ItemRequest"
self.textKey = "what"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "ItemRequest")
query.orderByAscending("what")
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! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
// Extract values from the PFObject to display in the table cell
if let what = object?["what"] as? String {
cell?.textLabel?.text = what
}
if let `where` = object?["`where`"] as? String {
cell?.detailTextLabel?.text = `where`
}
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:
NSIndexPath) {
self.performSegueWithIdentifier("showDetails", sender: tableView)
}
var detailScene = segue.destinationViewController as! DetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let row = Int(indexPath.row)
detailScene.currentObject = (objects?[row] as! PFObject)
}
}
}
And the code for the detail view:
import UIKit
import Parse
class DetailViewController: TableView {
var currentObject : PFObject?
@IBOutlet var whatis: UILabel!
@IBOutlet var whereis: UILabel!
@IBOutlet var whenis: UILabel!
@IBOutlet var whois: UILabel!
@IBOutlet var priceis: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if let object = currentObject {
whatis.text = (object["what"] as! String)
whereis.text = (object["`where`"] as! String)
priceis.text = (object["price"] as! String)
whenis.text=(object["when"] as! String)
}
}
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I run, the table shows only the "what" data points, and nothing else. When I try to select a row, it does not segue to the next screen (and I did segue set up in the storyboard). No error appears. Any help would be greatly appreciated- new to Swift and programming in general!
Update: tried the code suggested below and got a Thread1 Signal Sigabrt error. Error log below:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "uB8-vY-KsT-view-Ky8-TR-c7F" nib but didn't get a UITableView.'
*** First throw call stack:
(
0 CoreFoundation 0x00c24746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x0295ea97 objc_exception_throw + 44
2 CoreFoundation 0x00c2466d +[NSException raise:format:] + 141
3 UIKit 0x01aeb772 -[UITableViewController loadView] + 289
4 ParseStarterProject 0x0010c41b -[PFQueryTableViewController loadView] + 51
5 UIKit 0x018e5aef -[UIViewController loadViewIfRequired] + 78
6 UIKit 0x018e6095 -[UIViewController view] + 35
7 UIKit 0x01f472cc -[_UIFullscreenPresentationController _setPresentedViewController:] + 75
8 UIKit 0x018ba4f1 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 113
9 UIKit 0x018f36cc -[UIViewController _presentViewController:withAnimationController:completion:] + 2134
10 UIKit 0x018f6382 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
11 UIKit 0x018f61d4 -[UIViewController presentViewController:animated:completion:] + 224
12 UIKit 0x018fb02b -[UIViewController _showViewController:withAction:sender:] + 213
13 UIKit 0x01b38d32 -[UIStoryboardShowSegue perform] + 143
14 UIKit 0x01d8c297 -[UIStoryboardSegueTemplate _perform:] + 217
15 UIKit 0x018e8330 -[UIViewController performSegueWithIdentifier:sender:] + 72
16 ParseStarterProject 0x00106dec _TFC19ParseStarterProject9LendItems9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 268
17 ParseStarterProject 0x00106e99 _TToFC19ParseStarterProject9LendItems9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 89
18 UIKit 0x0189c2a6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1559
19 UIKit 0x0189c451 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 285
20 UIKit 0x018a1795 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
21 UIKit 0x017ab862 ___afterCACommitHandler_block_invoke + 15
22 UIKit 0x017ab80d _applyBlockToCFArrayCopiedToStack + 415
23 UIKit 0x017ab622 _afterCACommitHandler + 549
24 CoreFoundation 0x00b4586e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
25 CoreFoundation 0x00b457b0 __CFRunLoopDoObservers + 400
26 CoreFoundation 0x00b3b1ea __CFRunLoopRun + 1226
27 CoreFoundation 0x00b3aa5b CFRunLoopRunSpecific + 443
28 CoreFoundation 0x00b3a88b CFRunLoopRunInMode + 123
29 GraphicsServices 0x03fea2c9 GSEventRunModal + 192
30 GraphicsServices 0x03fea106 GSEventRun + 104
31 UIKit 0x01781106 UIApplicationMain + 1526
32 ParseStarterProject 0x000fe524 main + 180
33 libdyld.dylib 0x030a2ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)