尝试从解析中显示UITableView上的数据

时间:2015-05-26 06:48:32

标签: ios swift parse-platform

我尝试将数据从解析显示到UITableView,但它只显示空白UITableView(未显示数据)

我有一个解析大学课程,以及universityEnrolledName专栏名称

这是代码

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.
}


 */

}

有没有人对大学班级(来自Parse)显示universityEnrolledName数据有任何建议?谢谢!

3 个答案:

答案 0 :(得分:2)

下面,

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

此方法用于在tableview中显示数字行。由于您返回0,这意味着您告诉表格视图您的表格应该为零行。

同样

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

默认情况下,表视图有一个部分,如果您明确提供某个值,它将被覆盖。

因此,在这两种情况下,您都应该返回一些大于零的正数。

下面的方法应该返回UITableViewCell,但你写了PFTableViewCell。所以改变它。

  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;
}

答案 1 :(得分:0)

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
}

参考: how-to-make-a-simple-table-view-with-ios-8-and-swift

这可能对您有所帮助:)。

答案 2 :(得分:0)

我不写swift所以原谅任何语法问题,但我希望你想要更像的东西:

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;
}