0

I've been trying to reload data after another view controller is done saving file and unwind to the tableview

By the way, since this tableview is created on UIViewController, I got this error after trying to put tableview.reload()

Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffedffbcfa8)

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    tableView.reloadData()
    return fileURLs.count
}

I guess doing it this way may cause an infinite loop and keep reloadData forever?

what is the proper way to implement this method?

edit

put

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    tableView.reloadData()
}

or

@IBAction func unwindToCSVList(sender: UIStoryboardSegue) {
    //tableView.reloadData()
}

Didn't work, either way, Is this related to my UITableView delegate?

Mr.Kushwaha
  • 491
  • 5
  • 14
JameS
  • 231
  • 1
  • 2
  • 11
  • You are unwinding the segue, that means you will have method for this in controller, So why don't you reload tableview in that method? – Dipak Kacha Nov 07 '17 at 05:29
  • @DipakKacha if I put tableView.reloadData() inside unwind action in VC1 it cause this error **Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'** – JameS Nov 07 '17 at 05:32
  • @DipakKacha okay, I solved that error turns out I forgot to add an outlet. Btw, it still not reload after unwind though – JameS Nov 07 '17 at 05:38
  • 2
    May be possible, that table is reloading but the array you are passing may not be updated. – Dipak Kacha Nov 07 '17 at 05:43
  • @DipakKacha that is another possibility and another issue. any suggestion?? – JameS Nov 07 '17 at 05:46
  • 1
    @DipakKacha yesss! I solved it. Thank it is as you said – JameS Nov 07 '17 at 05:47
  • 1
    As @DipakKacha said "array you are passing may not be updated", you should use delegation method to pass updated data to the First Controller and then update that data inside array and reload the table afterwards OR re-call the API. – Bista Nov 07 '17 at 05:48
  • Are you updating array based on unwinded controller? So debug and check whether it's updating or not. Or it's updated after table reloaded? – Dipak Kacha Nov 07 '17 at 05:49
  • 1
    @Mr.Bista, when he's using unwind segue, and we can pass anything from segue. So why to use delegate or any other extra code? – Dipak Kacha Nov 07 '17 at 05:51
  • Using this `(sender: UIStoryboardSegue)`? – Bista Nov 07 '17 at 05:53
  • 1
    @Mr.Bista, yeah exactly. – Dipak Kacha Nov 07 '17 at 05:55
  • @JameS, congrats. It's all your effort. – Dipak Kacha Nov 07 '17 at 05:56

1 Answers1

1

tableView.reloadData() causes a call to numberOfRowsInSection and vice-versa. You guessed it right: infinite loop.

You can do the following:

  1. Create a delegate, which will be conformed by the First Controller.
  2. Second Controller will call the delegate method and reload the table inside that.

Delegate example: get indexPath of UITableViewCell on click of Button from Cell

Or easy way.

Call tableView.reloadData() inside ViewWillAppear or viewDidAppear method.

Bista
  • 7,869
  • 3
  • 27
  • 55
  • put tableView.reloadData() in either viewWillAppear or viewDidAppear still not reload after unwind from VC2 – JameS Nov 07 '17 at 05:42
  • 1
    If an unwind segue from VC2 calls an action (A) in VC1 then put tableView.reloadData() in action A. – laxman khanal Nov 07 '17 at 06:50