iOS SWIFT:拉动刷新功能不会更新TableView

时间:2015-08-18 19:06:06

标签: ios iphone swift uitableview parse-platform

我在TableView Controller中有刷新功能,它列出了所有用户名和当前用户关注的对象(由每行末尾的复选标记表示)。这是我的刷新功能:

  var refresher: UIRefreshControl!
    func refresh() {
    var query = PFUser.query()
    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let users = objects {
            self.usernames.removeAll(keepCapacity: true)
            self.userids.removeAll(keepCapacity: true)
            self.isFollowing.removeAll(keepCapacity: true)

            for object in users {
                if let user = object as? PFUser {
                    if user.objectId! != PFUser.currentUser()?.objectId {
                        self.usernames.append(user.username!)
                        self.userids.append(user.objectId!)

                        var query = PFQuery(className: "followers")
                        query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
                        query.whereKey("following", equalTo: user.objectId!)
                        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                            if let objects = objects {
                                if objects.count > 0 
                                {
                                self.isFollowing[user.objectId!] = true
                                }
                                 else 
                                {
                                 self.isFollowing[user.objectId!] = false

                                }
                            }

                      if self.isFollowing.count == self.usernames.count
                          {
                                 self.tableView.reloadData()
                                self.refresher.endRefreshing()

                            }
                        })

                    }
                }

            }

        }

    })

}

在我的viewDidLoad中调用它:

  override func viewDidLoad() {
    super.viewDidLoad()
    //pull to refresh funcationlity
    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refresher)
    refresh()

}

问题是,当我从Parse.com数据库手动删除项目,并在列表视图中拉到刷新时,它不会更新。这是我的tableview函数:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = usernames[indexPath.row]
    let followedObjectId = userids[indexPath.row]
    if isFollowing[followedObjectId] == true {
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
    }

       return cell
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
            let followedObjectId = userids[indexPath.row]
    if isFollowing[followedObjectId] == false {
        isFollowing[followedObjectId] = true
        cell.accessoryType = UITableViewCellAccessoryType.Checkmark
        var following = PFObject(className: "followers")
        following["following"] = userids[indexPath.row]
        following["follower"] = PFUser.currentUser()?.objectId
        following.saveInBackground()
    } 
   else 
     {
        isFollowing[followedObjectId] = false
        cell.accessoryType = UITableViewCellAccessoryType.None
        var query = PFQuery(className: "followers")
        query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
        query.whereKey("following", equalTo: userids[indexPath.row])
        query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
            if let objects = objects {
                for object in objects {
                    object.deleteInBackground()
                    print(object)
                }
            }
        })
    }
}

0 个答案:

没有答案