moez05
moez05

Reputation: 137

Trying to display data on a UITableView from parse

I'm trying to display data from parse onto a UITableView but it's only displaying a blank UITableView (no data being shown)

I have a University class in parse, as well as a universityEnrolledName column name

here is the code

import UIKit
import Parse
import ParseUI
class viewUniversityList: PFQueryTableViewController {


@IBOutlet var uiTableView: UITableView!

override init(style: UITableViewStyle, className: String!){
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)

    self.parseClassName = "University"
    self.textKey = "universityEnrolledName"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false

}


override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "University")
    query.orderByAscending("universityEnrolledName")
    return query;
}


override func viewDidLoad() {
    super.viewDidLoad()

    uiTableView.delegate = self
    uiTableView.dataSource = self

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0
}


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")
    }

    if let universityEnrolledName = object?["universityEnrolledName"] as? String{
        cell?.textLabel?.text = universityEnrolledName
    }

    if let classEnrolledName = object?["classEnrolledName"] as? String{
        cell?.detailTextLabel?.text = classEnrolledName
    }


    return cell;
}


/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


 */

}

does anyone have any advice on displaying the universityEnrolledName data from the University class (from Parse)? Thanks!

Upvotes: 2

Views: 331

Answers (3)

Wain
Wain

Reputation: 119041

I don't write swift so forgive any syntax issues, but I expect you want something more like:

import UIKit
import Parse
import ParseUI

class viewUniversityList: PFQueryTableViewController {


@IBOutlet var uiTableView: UITableView!

override init(style: UITableViewStyle, className: String!){
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)

    self.parseClassName = "University"
    self.textKey = "universityEnrolledName"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

override func viewDidLoad() {
    super.viewDidLoad()

    uiTableView.delegate = self
    uiTableView.dataSource = self

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "University")
    query.orderByAscending("universityEnrolledName")
    return query;
}

override func textKey() -> NSString {
    return "universityEnrolledName"
}

// MARK: - Table view data source

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

    var cell = super.tableView(tableView, dequeueReusableCellWithIdentifier:indexPath, object:object) as! PFTableViewCell!

    if let classEnrolledName = object?["classEnrolledName"] as? String{
        cell?.detailTextLabel?.text = classEnrolledName
    }

    return cell;
}

Upvotes: 0

Amit89
Amit89

Reputation: 3000

Here,

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0
}

This method is used to display number rows in a tableview. Since you are returning 0, which means you are telling to your table view that your table should have zero row's.

Similarly

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0
}

By default table view have one section, if you are explicitly providing some value it will be overridden.

So in both case you should return some positive number greater than zero.

The below method should return UITableViewCell, but you wrote PFTableViewCell. So change it.

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> UITableViewCell {


    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
    if cell == nil {
        cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    if let universityEnrolledName = object?["universityEnrolledName"] as? String{
        cell?.textLabel?.text = universityEnrolledName
    }

    if let classEnrolledName = object?["classEnrolledName"] as? String{
        cell?.detailTextLabel?.text = classEnrolledName
    }
    return cell;
}

Upvotes: 2

Yuyutsu
Yuyutsu

Reputation: 2527

override func viewDidLoad() {
    super.viewDidLoad()

//Conform to the TableView Delegate and DataSource protocols 

    uiTableView.delegate = self //set delegate
    uiTableView.dataSource = self // set datasource
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 0 //set count of section
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0 //set count of rows
}

Refere this: how-to-make-a-simple-table-view-with-ios-8-and-swift

This might helps you :)

Upvotes: 0

Related Questions