简体   繁体   English

Swift解析查询结果未出现在TableView中

[英]Swift parse query results not appearing in tableview

I am running into difficulty displaying the data from the query I made in the individual cells of my tableview. 我在显示来自我在tableview的单个单元格中进行的查询的数据时遇到了困难。 I believe that my logic is correct, but I'm not seeing the console.log's that I am calling within my function that contains the Parse queried data. 我相信我的逻辑是正确的,但是我看不到正在包含解析查询数据的函数中调用的console.log。 This might be a simple fix, but it isn't coming to me at the moment. 这可能是一个简单的解决方法,但目前还没有解决。 The console log I should be seeing to validate that my query is coming through correctly is the println("\\(objects.count) users are listed") , it should then be displayed within the usernameLabel.text property. 我应该看到用来验证查询是否正确通过的控制台日志是println("\\(objects.count) users are listed") ,然后应该在usernameLabel.text属性中显示该日志。

import UIKit

class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var userArray : NSMutableArray = []

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        loadParseData()

    }

    func loadParseData() {
        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {
                if let objects = objects { 
                    println("\(objects.count) users are listed")
                    for object in objects {
                        self.userArray.addObject(object)
                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }

    let textCellIdentifier = "Cell"

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.userArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell
        let row = indexPath.row
        let cellDataParse : PFObject = self.userArray.objectAtIndex(row) as! PFObject

        //cell.userImage.image = UIImage(named: usersArr[row])
        cell.usernameLabel.text = cellDataParse.objectForKey("_User") as! String

        return cell
    }
}

I fixed the issue. 我解决了这个问题。 I needed to cast the index path row in the users array as a PFUser and then cast the user's username property as a String and then set that as the label text. 我需要将users数组中的索引路径行PFUserPFUser ,然后将用户的username属性PFUser为String,然后将其设置为标签文本。

        let row = indexPath.row

        var user = userArray[row] as! PFUser
        var username = user.username as String

        cell.usernameLabel.text = username

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM