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

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

问题描述

我在显示我在tableview的单个单元格中查询的数据时遇到了困难.我相信我的逻辑是正确的,但是我看不到正在包含解析查询数据的函数中调用的console.log.这可能是一个简单的解决方法,但目前还没有解决.我应该看到用来验证查询是否正确通过的控制台日志是println("\(objects.count) users are listed"),然后应将其显示在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
    }
}

推荐答案

我已修复此问题.我需要将users数组中的索引路径行强制转换为PFUser,然后将用户的username属性转换为String,然后将其设置为标签文本.

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天全站免登陆