Edoardo Riggio
Edoardo Riggio

Reputation: 83

Refreshing data using Parse

I have a problem. I need that when I press a button this will automatically refresh my page putting inside it the new content. Possibly while this happens, I would like the button to do a circle animation. Thanks in advance.

class ViewControllerAvvisi: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate
{

var selfTable: NSMutableArray = NSMutableArray()

@IBOutlet weak var MessageTable: UITableView!
@IBOutlet weak var MessageTextField: UITextField!
@IBOutlet weak var SendMessage: UIButton!

var messagesArray:[String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.MessageTable.delegate = self
    self.MessageTable.dataSource = self

    self.MessageTable.delegate = self

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TableViewTapped")
    self.MessageTable.addGestureRecognizer(tapGesture)

    func retrieveMessages() {

        let query = PFQuery(className: "Message")

        query.findObjectsInBackgroundWithBlock {
            (remoteObjects: [PFObject]?, error: NSError?) -> Void in

            self.messagesArray = [String]()

            for messageObject in remoteObjects! {

                let messageText: String? = (messageObject as PFObject) ["Text"] as? String

                if messageText != nil {

                    self.messagesArray.append(messageText!)

                }
            }

            self.MessageTable.reloadData()

        }
    }

    retrieveMessages()


}

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

    let cell = self.MessageTable.dequeueReusableCellWithIdentifier("MessageCell")! as UITableViewCell

    cell.textLabel?.text = self.messagesArray[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return messagesArray.count
}

@IBAction func SendMessage(sender: AnyObject) {

    self.MessageTable.endEditing(true)

    let NewMessage: PFObject = PFObject (className: "Message")
    NewMessage["Text"] = self.MessageTextField.text

    self.MessageTextField.enabled = false
    self.SendMessage.enabled = false

    NewMessage.saveInBackgroundWithBlock { (success:Bool, NSError) -> Void in

            if (success == true) {

                NSLog("Message successfully saved")

        }else{

                NSLog("Message didn't save")

        }

    self.MessageTextField.enabled = true
    self.SendMessage.enabled = true
    self.MessageTextField.text = ""

    }
}

@IBAction func Refresh(sender: AnyObject) {

}

}

Upvotes: 0

Views: 74

Answers (1)

Antoine
Antoine

Reputation: 1149

Just use the function you created to reload the new data. Move it outside of the viewDidLoad function. Add an IBAction with the button you want to reload data and add retrieveMessages(). As you reload, new data will automatically be loaded.

@IBAction func refreshData(sender: AnyObject) {
retrieveMessages()
}

For the circle animation, I recommend using a UIActivityIndicatorView.

Upvotes: 2

Related Questions