快速将PFQuery转换为TableView的字符串数组 [英] Swift Converting PFQuery to String Array for TableView

查看:93
本文介绍了快速将PFQuery转换为TableView的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询数据库中的所有Parse用户,然后在表格视图中在其自己的单元格中显示每个单独的用户.我已经设置了表格视图,但是我坚持将用户查询保存到可以在表格视图中使用的字符串数组中.我创建了一个loadParseData函数,该函数在后台查找对象,然后将查询到的对象附加到字符串数组中.不幸的是,我在追加数据的行上收到一条错误消息.

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. 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. 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'在我看来,这是建议我使用self.而不是usersArray.,因为这是在闭包内,但是如果以这种方式运行它,则会出现另一个错误,*classname* does not have a member named 'append'

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. instead of 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'

这是我的代码:

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
    }


}

推荐答案

问题是userArray是一个NSArray. NSArray是不可变的,这意味着它无法更改.因此,它没有附加功能.您想要的是一个NSMutableArray,可以更改它并具有addObject函数.

The problem is that userArray is an NSArray. NSArray is immutable, meaning it can't be changed. Therefore it doesn't have an append function. What you want is an NSMutableArray, which can be changed and has an addObject function.

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

此外,由于对象以"AnyObject"的形式返回,因此您必须在某些时候将它们强制转换为PFUser才能使用它们.只是要记住一点

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. Just something to keep in mind

用于获取用户名并显示该用户名

//将其放入cellForRowAtIndexPath

// Put this in cellForRowAtIndexPath

var user = userArray[indexPath.row] as! PFUser
var username = user.username as! String
cell.usernameLabel.text = username

这篇关于快速将PFQuery转换为TableView的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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