views:

27

answers:

1

I asked a question the other day about how rows are added to a UITableView after the table is shown and I was told that the datasource should be updated and insertIndexPaths:withRowAnimation should be called.

My UITable view contains rows of data, when the row is clicked another screen is shown which allows the user to edit the data, once they click done for that screen I want the data in the row to be updated too. I understand calling reloadData is not the right approach here.

Going back a step or two, how does the UITableView view know when it is returned to be the edit screen ? and which event do handle datasource / row updates?

I'm also going to want to add new row (in and add view) in a similar manner, which event do i use then ?

+2  A: 

You can keep track with -viewWillAppear:animated:. This works with any view controller. Reload your data in there. So the process is:

  • Edit your data
  • Save the new data in your model (where the table view is getting its data)
  • Pop the detail view controller
  • In the table view controller, have a viewWillAppear:animated: method where you reload your table (using reloadData)
nevan
I was told you should NEVER reloadData once the table has been loaded as it looks awful.
Jules
I'm also getting this warning and a crash warning: Cancelling call - objc code on the current thread's stack makes this unsafe. That on the line where I assign my data array to the data source array
Jules
Notice that you're having two view controllers, you should call reloadData on the first view controller's viewWillAppear method. Not the second one, where you are editing your data. Nevan is right, follow his instruction carefully.
sfa
No I'm not calling the reloadData at all, it would be afterwards that I would call it. I'm updating my datasource in viewWillApear now. Thats whats causing the error.
Jules
To recap, I've moved all code from viewDidLoad method (sqlite3, get records from the db, to a temporary array, thats assigned my datasource array). I then followed nevan's advice and moved code to viewWillAppear. While assigning my dataSource array to my temporary array I get the warning 'Cancelling call - objc code on the current thread's stack makes this unsafe.' my app then crashes with EXC_BAD_ACCESS. So my question is how do I assign my datasource correctly, before I call reloadData ?
Jules
That should read... my temporary array to dataSource array
Jules
If you have a table view and you change its data, you should call `reloadData`. Whoever told you that you shouldn't is wrong. That person might have thought that you shouldn't call it while the table view is on screen. If you are loading data from a slow source and calling `reloadData` each time, it can be slow. In my answer, you call it before the table view come back on screen (`viewWillAppear`) so there should be no problem.
nevan
OK, I was mistaken, your correct. However, I'm having the unsafe error while updating my datasource, what can i do ?
Jules
- (void)viewWillAppear:(BOOL)animated { NSMutableArray *arrayTmp= [[NSMutableArray alloc] init]; //populate array self.transactionsArray = arrayTmp; [arrayTmp release];
Jules
It looks like your problem is more with how you are saving your data.
nevan
How do you mean ?
Jules
Your original question is about reloading data and views, but it seems like the problem you have is how you're saving the data. You should open a new question with code samples.
nevan
The error is happening now as a result of following your advice, the error didn't happen before.
Jules
OK, I will accept this question, I've commented out my sqlite3 code and put a simple addObject to populate the array it it work, very odd :(
Jules