0

The title sounds a bit more confuesing as it is.

I have an UITableView where I instantiate a UINib which holds a UITableView. This TableView lets call it "DetailTableView" displays some information which can be added in another ViewController - so far so good.

What I want to achieve is that this "DetailTableView" automatically updates after I added a new entry.

The entry is saved in a local Realm database and is already displayed correctly but the "DetailTableView" only updates when I force close and reopen the app.

I already tried to reload the UINib and fire a tableview.reloadData() with Notification which posts in viewWillAppear() but I can't get that thing to work. I also tried to fire instatiate:WithOwner or awakeFromNib() but that didn't worked as well.

I have searched through the web a lot and also on here but cant find any answer I hope you guys can help me. thanks a lot!

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
Michael
  • 5
  • 4

1 Answers1

0

You should use this code

data = try? realm.objects(Model.self)

token = data?.observe { (changes: RealmCollectionChange) in

                switch changes {
                case .initial:
                    self.tableView.reloadData()
                    break
                case .update:
                    self.tableView.reloadData()
                    break
                case let .error(error):
                    fatalError("\(error)")
                    break
                }
            }
Vadim Kozak
  • 420
  • 3
  • 11
  • the original tableView where you can add entries is updating fine the problem is, that I have another ViewController where there is a UINib and in this UINib there is a tableView populated with the same data but filtered and this tableView in the UINib does not update - only if I reopen the app – Michael May 24 '18 at 13:52
  • as it is a UINib I have done it like this: if let listView = Bundle.main.loadNibNamed("ListView", owner: self, options: nil)?.first as? Listview { listView.tableview.reloadData() } But either this nor other thinks like: listView.newNew.instantiate(withOwner: self, options: nil) listView.todoView.setNeedsDisplay() listView.todoView.setNeedsLayout() listView.awakeFromNib() ...worked. – Michael May 24 '18 at 14:03