简体   繁体   English

快速将PFQuery转换为TableView的字符串数组

[英]Swift Converting PFQuery to String Array for TableView

I am trying to query all of the Parse users in my database and then display each individual user in their own cell in a tableview. 我正在尝试查询数据库中的所有Parse用户,然后在表视图中在其自己的单元格中显示每个用户。 I have set up my tableview, but I'm stuck on saving the user query to a string array that can be used within the tableview. 我已经设置了表格视图,但是我坚持将用户查询保存到可以在表格视图中使用的字符串数组中。 I have created a loadParseData function that finds the objects in the background and then appends the objects queried to a string array. 我创建了一个loadParseData函数,该函数在后台查找对象,然后将查询到的对象附加到字符串数组中。 Unfortunately I am given an error message on the line where I append the data. 不幸的是,我在追加数据的行上收到一条错误消息。

Implicit user of 'self' in closure; use 'self.' to make capture semantics explicit' Implicit user of 'self' in closure; use 'self.' to make capture semantics explicit' This seems to me that it is suggestion that I use self. Implicit user of 'self' in closure; use 'self.' to make capture semantics explicit'在我看来,这是我使用self.建议self. instead of usersArray. 而不是usersArray. because this is within a closure, but I'm given another error if I run it that way, *classname* does not have a member named 'append' 因为这是在闭包内,但是如果以这种方式运行它,我会遇到另一个错误, *classname* does not have a member named 'append'

Here is my code: 这是我的代码:

import UIKit

class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userArray = [String]()

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

    }

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


    func loadParseData(){

        var query : PFQuery = PFUser.query()

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

            if error != nil{

                println("\(objects.count) users are listed")

                for object in objects {

                    userArray.append(object.userArray as String)

                }
            }

        }

    }


    let textCellIdentifier = "Cell"

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        //cell.userImage.image = UIImage(named: usersArray[row])

        //cell.usernameLabel?.text = usersArray[row]

        return cell
    }


}

The problem is that userArray is an NSArray. 问题在于userArray是一个NSArray。 NSArray is immutable, meaning it can't be changed. NSArray是不可变的,这意味着它无法更改。 Therefore it doesn't have an append function. 因此,它没有附加功能。 What you want is an NSMutableArray, which can be changed and has an addObject function. 您需要的是NSMutableArray,可以对其进行更改并具有addObject函数。

var userArray:NSMutableArray = []

func loadParseData(){
    var query : PFQuery = PFUser.query()
    query.findObjectsInBackgroundWithBlock {
        (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil {
            if let objects = objects {
                for object in objects {
                    self.userArray.addObject(object)
                }
            }
            self.tableView.reloadData()
        } else {
            println("There was an error")
        }
    }
}

Also, because objects are returned as 'AnyObject' you will have to cast them as PFUsers at some point in order to use them as such. 另外,由于对象以“ AnyObject”的形式返回,因此您必须在某些时候将它们强制转换为PFUser才能使用它们。 Just something to keep in mind 只是要记住的一点

For getting the user's username and displaying it 用于获取用户的用户名并显示它

// Put this in cellForRowAtIndexPath //将其放在cellForRowAtIndexPath中

var user = userArray[indexPath.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