因此,我正在尝试开发一个应用程序,使人们可以看到他们所在的组。因此,对于他们已经建立或添加到的每个组,它将被保存在一个名为“ grupper”的类中。他们的用户名保存在“用户名”列中,并位于“ gruppe”组中。

我已经搜索了一段时间的脚本,该脚本可以在表视图中检索其组。我没有收到任何错误,但表视图只是空白。

我无法指出为什么它不检索它们所在的任何名称。

我对stackoverflow和Swift非常陌生,所以如果需要提供更多信息,请告诉我。

这是我的代码:

@IBOutlet weak var grupper: UITableView!

var userArray: [String] = []

    func retrieveMessages() {
        let userArray: [String] = []
        let query:PFQuery = PFQuery(className: "grupper")
        let currentUser = query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
        currentUser.findObjectsInBackgroundWithBlock {
            (objects, error) -> Void in

            for object in objects! {
                let username:String? = (object as PFObject)["gruppe"] as? String
                if username != nil {
                    self.userArray.append(username!)
                }
            }
            self.grupper.reloadData()
        }
    }

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

    // MARK: - Table view data source

     func numberOfSectionsInTableView(gruppe: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

     func grupper(grupper: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return userArray.count
    }


     func grupper(grupper: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // Update - replace as with as!

        let cell = grupper.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = userArray[indexPath.row]

        return cell
    }

最佳答案

目前尚不清楚何时以及从哪个方法调用您的retrieveMessages方法。

首先,确保从retrieveMessages(第一次访问视图时调用)或viewDidLoad(每次访问视图时调用)调用viewWillAppear

例如:

class YourController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        retrieveMessages()
    }

    // ... your other code is omitted for brevity ...

}


其次,如果您已经在执行上述操作(或类似的操作),请确保retrieveMessages方法中的查询正在返回数据。

您可以添加一条临时的print行以检查userArray数组的大小。 (或者,您可以使用调试器进行检查。)

例如:

func retrieveMessages() {
   // ... your code is omitted for brevity ...
   print("userArray size = \(userArray.count)")
}

关于ios - 正在从解析数据库中检索数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34844743/

10-10 20:52
查看更多