-1

I would like to do a self.tableView.reloadData() in an external class, not in the tableViewController. I found that I have to do a custom delegate in my class but I can't figure out how to do that! Someone can show me with some code example in Swift how to do it, thanks!

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
Luca Alberto
  • 1,195
  • 3
  • 11
  • 30
  • to reload data of a table view you dont need to do it in a delegate. you can do it from everywhere where you can access the table view. so if you pass the tableview to an object, it can reload that, no matter if it is implementing the delegate or not. though I'd always advice to have the dlegate/datasource implementation not in the view controller. Over all your question is quite vague. I vote for closure. – vikingosegundo Dec 17 '15 at 18:12
  • @vikingosegundo No, I can't pass the tableView in my object and do the tableView.reloadData, it doesn't work for me! – Luca Alberto Dec 17 '15 at 20:12
  • and why doenst it work? be more specific and post code that isnt working for you. – vikingosegundo Dec 17 '15 at 20:42
  • please don't repost question, like this: http://stackoverflow.com/questions/34262964/do-tableview-reloaddata-in-external-class – vikingosegundo Dec 17 '15 at 22:48
  • @vikingosegundo this is more specific? http://stackoverflow.com/questions/34262964/do-tableview-reloaddata-in-external-class – Luca Alberto Dec 18 '15 at 07:35
  • No, but you posted the same question twice. dont do that. – vikingosegundo Dec 18 '15 at 08:10

1 Answers1

0

You can write your protocol or just make easier way to do with NSNotification like that;

class xViewController: UIViewController {

    let myTableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), style: .Plain)

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadMyTableView:", name: "updateNotification", object: nil)
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        NSNotificationCenter.defaultCenter().removeObserver(self, name: "updateNotification", object: nil)
    }

    func reloadMyTableView(notification: NSNotification) {
        myTableView.reloadData()
    }
}

And post your notification from where do you want to reload;

class yViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().postNotificationName("updateNotification", object: nil)
    }
}
Kemal Can Kaynak
  • 1,638
  • 14
  • 26
  • I'm trying to do a PFQuery from Parse to fill the array that will populate my tableView. I'm doing this with findObjectInbackground like that: `query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in if error == nil { // The find succeeded...` I put the NSNotification into the query to do the reloadData but it doesn't work! – Luca Alberto Dec 17 '15 at 20:16