Search code examples
iosuitableviewswiftpfquery

Table View Cell Reloading Issue


I have a table view in my Chat app that holds Users that are logged in to the application. This app is in Swift and the table view is embedded in a Navigation controller. I'm also using Parse.

When I click on a User, it sends me to a chat screen which it's suppose to do. Then when I click the Back button, it takes me back to the User table view as it should, but something strange happens. It has the Users that are logged in, but shows them twice. For example, If User1 is logged in to the app, I click on it to chat, then go back to the table view, it now shows User1 twice. If I repeat the process, it then shows User1 three times. Hopefully someone can help...

Variables:

 import UIKit

   // Global Variable 
   var userName = ""

class usersVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var resultsTable: UITableView!

var resultsUsernameArray = [String]()
var resultsProfileNameArray = [String]()
var resultsImageFile = [PFFile]()

override func viewDidLoad() {
    super.viewDidLoad()

    let theWidth = view.frame.size.width
    let theHeight = view.frame.size.height

    resultsTable.frame = CGRectMake(0, 0, theWidth, theHeight-64)

    // PFUser.currentUser().username is part of Parse framework
    userName = PFUser.currentUser()!.username!

}

Then here is the viewDidAppear where I believe is the issue:

    override func viewDidAppear(animated: Bool) {

    let predicate = NSPredicate(format: "username != '"+userName+"'")
    var query = PFQuery(className: "_User", predicate: predicate)
    var theObjects = query.findObjects()
        for object in theObjects! {

        // object.username is the name of the username class in Parse, as well as "profileName" and "photo"
        self.resultsUsernameArray.append(object["username"] as! String)
        self.resultsProfileNameArray.append(object["profileName"] as! String)
        self.resultsImageFile.append(object["photo"] as! PFFile)

        self.resultsTable.reloadData()

    }



}

Not sure if this is needed but it had some of the same variables and deals with the Table View:

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

    var cell : ResultsCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ResultsCell

    cell.usernameLbl.text = self.resultsUsernameArray[indexPath.row]
    cell.usernameLbl.hidden = true

    cell.profileNameLbl.text = self.resultsProfileNameArray[indexPath.row]


    resultsImageFile[indexPath.row].getDataInBackgroundWithBlock {

        (imageData: NSData?, error: NSError?) -> Void in

        if error == nil {

            let image = UIImage(data: imageData!)
            cell.profileImg.image = image
        }
    }

    return cell

}

Let me know if more code is needed!

Thank you, Jack


Solution

  • You should change your viewDIdAppear() method little bit way like this,

    override func viewDidAppear(animated: Bool) {
        let predicate = NSPredicate(format: "username != '"+userName+"'")
        var query = PFQuery(className: "_User", predicate: predicate)
        var theObjects = query.findObjects()
        self.resultsUsernameArray.removeAll(keepCapacity: true)
        self.resultsProfileNameArray.removeAll(keepCapacity: true)
        self.resultsImageFile.removeAll(keepCapacity: true)
        for object in theObjects! {
    
            // object.username is the name of the username class in Parse, as well as "profileName" and "photo"
            self.resultsUsernameArray.append(object["username"] as! String)
            self.resultsProfileNameArray.append(object["profileName"] as! String)
            self.resultsImageFile.append(object["photo"] as! PFFile)
    
            self.resultsTable.reloadData()
    
        }
    }
    

    HTH, Enjoy Coding !!