Table 查看单元格重新加载问题

Table View Cell Reloading Issue

我的聊天应用程序中有一个 table 视图,其中包含登录到该应用程序的用户。此应用程序位于 Swift 中,并且 table 视图嵌入在导航控制器中。我也在使用 Parse。

当我点击一个用户时,它会把我带到它应该做的聊天屏幕。然后,当我单击“后退”按钮时,它会按原样返回到 User table 视图,但奇怪的事情发生了。它有登录的用户,但显示了两次。例如,如果 User1 登录到应用程序,我点击它聊天,然后返回 table 视图,它现在显示 User1 两次。如果我重复该过程,它会显示 User1 三次。希望有人能帮忙...

变量:

 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!

}

然后这是我认为问题所在的 viewDidAppear:

    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()

    }



}

不确定是否需要,但它有一些相同的变量并处理 Table 视图:

 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

}

如果需要更多代码,请告诉我!

谢谢, 杰克

你应该像这样稍微改变你的 viewDIdAppear() 方法,

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,享受编码吧!