I am using parse and I have a PFQueryTableViewController inside a scrollView. When I load the app the data shows for a brief moment and then disappears. After I try to refresh the data the app immediately crashes. I think it has something to do with the permissions/delegate of the subViews because the same code works when the table is not inside the scrollView. The code for the table is just the default code pointing to my data
import UIKit
import Parse
import ParseUI
class CustomTableViewControllerA: PFQueryTableViewController {
override init(style: UITableViewStyle, className: String!){
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
//
self.parseClassName = "Countries"
self.textKey = "nameEnglish"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("myCell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell")
}
if let nameEnglish = object?["nameEnglish"] as? String {
cell?.textLabel?.text = nameEnglish
}
if let capital = object?["capital"] as? String {
cell?.detailTextLabel?.text = capital
}
return cell
}
}
And the scrollView is made using storyboard in a CustomViewController
var AVC:CustomTableViewControllerA = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("CustomTableViewControllerA") as! CustomTableViewControllerA;
.
self.scrollView.addSubview(AVC.view)
and
self.scrollView.delegate = self
The frame settings for the scrollView/subViews are correct and I have not had a problem with that part of the code. The code runs without crashing when there is no internet connection, it just stays in a permanent refresh state.
Related
I put my working tableviewcontroller inside a pageviewcontroller. Now I have to register the cell in the tableview programmatically and the method awakeFromNib is not called for the cell. All properties in my custom cell are not initialized and the app crashes if I try to set content.
Is there anything different when I add a tableviewcontroller in a pageviewcontroller?
self.tableView.registerClass(ParticipantCell.self, forCellReuseIdentifier: ParticipantCell.cellIdentifier)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let index = self.sections[indexPath.section].index+indexPath.row
let participant = self.participants[index]
let cell = tableView.dequeueReusableCellWithIdentifier(ParticipantCell.cellIdentifier, forIndexPath: indexPath) as! ParticipantCell
// cell.setParticipantData(participant)
return cell
}
If you are creating cells programmatically awakeFromNib is not called. However you can do your configurations by overriding:
init(style: UITableViewCellStyle, reuseIdentifier: String?)
As a suggestion you can have a method like this:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupCell()
}
func setupCell() {
// setup your cell here
}
The problem was that the Tableviewcontroller was instantiated wrong. I solved it by instantiating it from the storyboard:
let storyboard = UIStoryboard(name: "Participants", bundle: NSBundle.mainBundle())
if let controller = storyboard.instantiateViewControllerWithIdentifier("ParticipantListController") as? ParticipantListController{
...
}
Im using PFQueryTableViewController to load all my data from parse.
Currently it loads all the data at once, so in the future if i have many data it will mess up the user experience of users. How do i load it 5 per 5 whenever I scrolled down?
Here's the code
import UIKit
import Parse
import ParseUI
class MainViewTable: PFQueryTableViewController {
// 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)
self.parseClassName = "product"
self.textKey = "createdAt"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "product")
query.orderByDescending("createdAt")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
if let titleName = object?["title"] as? String {
cell.title.text = titleName
}
if let priceTitle = object?["price"] as? Int {
cell.price.text = String(priceTitle)
println(cell.price.text! + "Price")
}
return cell
}
override func viewDidAppear(animated: Bool) {
// Refresh the table to ensure any data changes are displayed
tableView.reloadData()
}
}
I'm trying to populate a table using PFQueryTableViewController.
I want to use data from two class, which is Places and Details.
In Places, I have two column, which is placeText as string, and pointerToDetails as pointer.
In Details, I have one column, which is detailText.
I want to show the placeText and detailText in the same CustomCell which I already defined as PFTableViewCell.
Unfortunately, after I run the code, I only got the placeText inside the CustomCell.
import UIKit
class TableViewController: PFQueryTableViewController {
// 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.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: "Places")
query.includeKey("pointerToDetails")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
cell.name.text = object["placeText"] as String!
cell.detail.text = object["detailText"] as String!
return cell
}
}
After I got an inspiration from #deadbeef (see answer 1), here is the solution I got :
query.includeKey("pointerToDetails") is querying an object which can be accessed via object["pointerToDetails"].
to extract data from column detailText which already included in object["pointerToDetails"], just do this :
if let pointer = object["pointerToDetails"] as? PFObject {
cell.detail.text = object["detailText"] as String!
}
here is the whole code :
import UIKit
class TableViewController: PFQueryTableViewController {
// 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.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: "Places")
query.includeKey("pointerToDetails")
return query
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
cell.name.text = object["placeText"] as String!
if let pointer = object["pointerToDetails"] as? PFObject {
cell.detail.text = object["detailText"] as String!
}
return cell
}
}
The detailtext property will not be included in your object as the includeKey() might suggest, but the Details object pointed by pointerToDetails will be queried along with you Places object.
So in order to get the value of detailText, you have to go through the pointer. In other words, try this :
cell.name.text = object["placeText"] as String!
cell.detail.text = object["pointerToDetails"]["detailText"] as String!
All,
I have got the cells working fine with a normal cell, but I am trying to create a custom cell with a nib etc and then link that into PARSE using PFQueryTableViewController.
Here is my code :
class CustomCell: PFTableViewCell
{
#IBOutlet var EventTypeImage: UIImageView!
#IBOutlet var titleLabel: UILabel!
}
class TableViewController: PFQueryTableViewController {
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Events_"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
//self.objectsPerPage = 5
//self.textKey = "TypeOfVenue_"
}
override func viewDidLoad() {
var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "CustomCell")
}
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: self.parseClassName)
if (objects.count == 0)
{
query.cachePolicy = kPFCachePolicyNetworkOnly
}
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var custom = CustomCell()
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as PFTableViewCell!
custom.titleLabel?.text = object["Title"] as NSString
return cell
}
So I have created a custom cell and linked it with the CustomCell Class at the top. In CellForRowatIndexPath. I have created a new instance of the CustomCell Class and got the titlelabel from it and then Casted it to NSstring and used PARSE to get the 'object'.
I don't have any errors. All I get on the tableview is a spinning wheel saying Loading.
Any advice would be brilliant.
Was having a similar problem, where the nib wasn't getting loaded. I then realized I could create the custom cell right in the prototype of the tableviewcontroller in the storyboard, which I did, and changed the class in the identity inspector as shown. Here QuestionCell would be your custom cell file.
I would separate CustomCell into a different and make sure you have that selected in the prototype identity inspector. As well, make sure the style is custom, and make sure any of the labels are connected in the storyboard with the iboutlets in the new CustomCell class.
Then, you can delete the
var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "CustomCell")
since you are using the prototype.
Then replace
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as PFTableViewCell!
with
var cell:CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as? CustomCell
and you should be good to go! Let me know if this works!
I also had a spinning loading wheel while adding initializer properties to this init method:
required init(coder aDecoder: NSCoder)
But doing this, instead, worked:
//class TableViewController: PFQueryTableViewController
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
self.parseClassName = "todo"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 30
self.tableView.estimatedRowHeight = 80
}
All I have added this into my appdelegate
var controller:PFQueryTableViewController = PFQueryTableViewController(className: "Types_")
self.window?.rootViewController = controller
println(self.window?.rootViewController)
I have created an new class in swift like this :
class TableViewController: PFQueryTableViewController {
override init!(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.parseClassName = "Types_"
self.pullToRefreshEnabled = true
self.paginationEnabled = true
self.objectsPerPage = 5
//self.textKey = "TypeOfVenue_"
}
override func queryForTable() -> PFQuery! {
var query = PFQuery(className: self.parseClassName)
if (objects.count == 0)
{
query.cachePolicy = kPFCachePolicyNetworkOnly
}
return query
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell!
{
var cellIdentifier = "eventCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
}
cell.textLabel?.text = object["TypeOfVenue_"] as NSString
cell.detailTextLabel?.text = object["Seating"] as NSString
return cell
}
But the new class does not execute, if i add a breakpoint in the class, it doesn't execute the breakpoint.
I have added a ViewController in the Storyboard, and linked the class to the view controller like this :
Anyone have any ideas ?
I created a TableView controller on my Storyboard, and removed the code in the app delegate to create the controller.
Then I added this class as the the class on the tableview controller, and it worked.