Search code examples
iosswiftuitableviewreloaddata

iOS: How to know reloadData() was completed its task?


I want to scroll to a given index (self.boldRowPath), but when I debug scrollToRow is performed before reloadData().

How to know reloadData has finished ?

func getAllTimeEvent()  {
    self.arrAllTimeEvent = ModelManager.getInstance().getAllTimeEvent(from: self.apportmentDateFrom, to: self.apportmentDateTo)
    self.tblTimeEvent.reloadData()
    self.tblTimeEvent.scrollToRow(at: self.boldRowPath ?? [0,0], at: .top, animated: true)
 }

Any help will be much appreciated.


Solution

  • Try doing this, it should work:

    self.tblTimeEvent.reloadData()
    DispatchQueue.main.async(execute: {
        self.tblTimeEvent.scrollToRow(at: self.boldRowPath ?? [0,0], at: .top, animated: true)
    })
    

    This will execute the scrollToRow on the main thread, that means after the reloadData is done (because it is on the main thread)