Home > OS >  Swift TableView Contents Disappear and Flash When Reloaded
Swift TableView Contents Disappear and Flash When Reloaded

Time:09-17

I'm building a chat app with a table view of various channels that people can chat on.

If a new message (channel) comes in, I update my data store, and then reload the table view. In this particular instance, if a user accepts an invite to a channel; that channel gets added to the data store.

Every time a new channel gets added, this method gets called:

 @objc func processChannels() {
        if self.airlockStore.unPinnedChannelDataObjects.isEmpty == true {
            return
        } else {
            let pinnedSection = self.tableViewAdaptor!.sections[0] as! TableViewAdaptorSection<ChannelTableViewCell, ChannelDataObject>
            let unPinnedSection = self.tableViewAdaptor!.sections[1] as! TableViewAdaptorSection<ChannelTableViewCell, ChannelDataObject>
            unPinnedSection.items = airlockStore.unPinnedChannelDataObjects
            pinnedSection.items = airlockStore.pinnedChannelDataObjects
        }
        updateTableView()
    }

And subsequently, this one gets called:

  @objc func updateTableView() {
        tableViewAdaptor?.tableView.isUserInteractionEnabled = true
        tableViewAdaptor?.tableView.reloadData()
    }

However, I consistently get a flash / the contents of the table view temporarily disappear before then being displayed in the manner expected.

Have read several posts regarding flashes when loading a table view, but haven't been able to solve this yet.

I was looking at reloading individual rows in the table view, but the problem is; there's no guarantee of where the index in my list of channels a new channel will be added to.

CodePudding user response:

Have you tried not reloading after every update of tableview?

On the other hand, it may not be important but why you set isUserInteractionEnabled = true so many times.

CodePudding user response:

Use this before and after adding/deleting row in table view:

tableview.beginupdates() 

// adding/deleting row 

tableview.endupdates()
  • Related