reload UITableView in UINib which is in an UITableView

117 views Asked by At

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!

1

There are 1 answers

2
Vadim Kozak On

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
                }
            }