0

I'm working on a message app with parse.

I would like to filter by sender the result of query but it duplicate sender for each messages.

I think it is possible to find same value on a array? (in this exemple clientsArray)

Any idea?

Below the code that I used.

var clientName = ""
var clientsArray:[String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    //Message List Query
    let messages = PFQuery(className:"Message")
    messages.whereKey("user", equalTo:PFUser.currentUser()!["retailer"]!)

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

        if error == nil {

            print("Successfully retrieved \(objects!.count) scores.")

            if let objects = objects as? [PFObject] {

                self.clientName.removeAll()

                for object in objects {

                    if let clientName = object["clientName"] as? String {
                        self.clientsArray.append(clientName)
                    }

                    self.tableView.reloadData()

                    print(self.clientsArray)

                }
            }
        } else {
            print(error)
        }
    }        
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return clientsArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("clientsCell", forIndexPath: indexPath)
    let clientName = clientsArray[indexPath.row]

    if let clientNameLabel = cell.viewWithTag(301) as? UILabel {
        clientNameLabel.text = clientName
    }

    return cell
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showChat" {

        if let destination = segue.destinationViewController as? newChatViewController {

            destination.clientName = clientsArray[(tableView.indexPathForSelectedRow?.row)!]

            //self.hidesBottomBarWhenPushed = true
        }
    }
}

enter image description here

Kibo
  • 1,784
  • 1
  • 12
  • 16

1 Answers1

0

It's really hard to answer without seeing what the data structure looks like. But I can tell you the problem with your code.

 if let clientName = object["clientName"] as? String {
     self.clientsArray.append(clientName)
 }

Your if-let statement is simply getting the object of ["clientName"], which is just your clients name. Then you're appending your clients name to your clientsArray object.

This is why you're just seeing luke.

I can help resolve the problem, but I need to know what your data's structure looks like. Can you print out an entire object?

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135