Swift 解析查询结果未出现在 tableview 中 [英] Swift parse query results not appearing in tableview

查看:20
本文介绍了Swift 解析查询结果未出现在 tableview 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示我在 tableview 的各个单元格中所做的查询中的数据时遇到了困难.我相信我的逻辑是正确的,但我没有看到我在包含 Parse 查询数据的函数中调用的 console.log.这可能是一个简单的修复,但目前我还没有想到.为了验证我的查询是否正确通过,我应该看到的控制台日志是 println("(objects.count) users are列出"),然后它应该显示在 中usernameLabel.text 属性.

I am running into difficulty displaying the data from the query I made in the individual cells of my 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. 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.

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

推荐答案

我解决了这个问题.我需要将用户数组中的索引路径行转换为 PFUser,然后将用户的用户名属性转换为字符串,然后将其设置为标签文本.

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.

        let row = indexPath.row

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

        cell.usernameLabel.text = username

这篇关于Swift 解析查询结果未出现在 tableview 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆