Deleting rows UItableview iphone using userdefaults - iphone

I need your help, maybe its something silly but I can't find the error.
I have a table view and an add button, when its clicked I show a modal view with a little form, when save button is pressed I send the data to the TableView controller to show it, also I'm using NSUserDefaults to save this data in an array.
My problem comes when, for example, I add 2 new rows and delete 1 of them, then when I add another, it shows the last row I delete instead of showing the one I just add.
I'm doing the saving and reading this way:
in viewDidAppear I read the NSUserDefaults and get the data if exist.
in the method that catches the data from the ModalView I save to userdefaults.
in commitEditing method I read userdefaults and then delete the row from the array and from the table and then save this change in userdefaults.
I don't know what I doing wrong, Can anybody help me with this?

Use [self.tableView reloadData]; in the viewWillAppear of that class. You can also reference that class in another view controller, create an object for it and call [classObject.tableView reloadData];
Always remember to reload the tableView after an add operation or a delete operation.

Don’t use NSUserDefaults to act as the direct datasource of a table. Store it in an intermediate model. It’ll be a LOT easier to debug that way.
If you’re having trouble keeping your model synced with the user defaults, call [[NSUserDefaults standardUserDefaults] synchronize] whenever you alter them.
Also, make sure you’re not making some mistakes in your table cell reuse. You might be seeing old data if you have a customer UITableViewCell class and you’re not correctly implementing -prepareForReuse.

Related

tableView not updating after adding Core Data object

Given this Core Data Model: Photographer <-->>Photos.
After adding a Photographer via a Modal View, the tableView of Photographers updates accordingly.
The problem is, that it doesn't do the same for Photos. I can add a Photo via a Modal View, and the tableView of Photos for that particular Photographer does not update immediately. Once I navigate back to the tableView of Photographers and select the same Photographer again, it updates as expected.
How can I get the tableView of Photos to update as soon as the Modal View(where the user added the object) gets dismissed?
Where modal view dismiss try to use:
[tableView reloadData];
Try
[tableview reload];
method after getting new data.
Check your error variable if it contains an error.
You directly assign the result of -executeFetchRequest:error: to an ivar (photographerPhotosArray). This either leaks or causes an immediate release.
Consider switching to NSFetchedResultsController since it helps you a lot.
Check your array first is it fill or empty? try with NSLog and check it. if it exist then reload your table

How to update UITableView at run time, after the view is loaded

I have read several articles about UITableView, including the official doc and some on SO. But my situation seems to be different.
I want to update the Table each time the view loaded. And I must fetch the data using HTTP request.
What I got now is:
When enter the table view, I should use a non-synchronous HTTP request to update the data. Because I don't want the main thread to wait. One place to do that, is in the tableView:cellForRowAtIndexPath: method. So I return 0 for no data exist at the beginning.
When I get the HTTP respond, I update rows on main thread using beginUpdates endUpdates insertRowsAtIndexPaths:withRowAnimation:
And I must update the "Data Source" at the same time, but how to do that?
Or should I make a daemon thread and update my data every once in a while? So that the data will be ready when TableView is loaded.
You would do it like this:
Have a boolean or some variable where you can reliably detect whether you have all the data.
In viewWillAppear, reset everything. Start loading your data.
If you don't have the data yet, you only display one section with one cell (a placeholder cell that reads "Loading..." and shows a spinner, for instance).
Once the data is completely loaded, you set the bool or whatever.
Call [self.tableView reloadData];
In all of your UITableViewDataSource methods you would need to check whether you've got the data already or not. If not, you return the placeholder data.
[yourtablename reloadData]; will help you relaod the data in the tableview, You can call this once you get the response from your server
I'm not sure there's a "best method" for what you're trying to accomplish here. I would suggest trying the method you have, and seeing if it provides an adequate user experience (whatever that means to you) and if it doesn't, try something else. I would definitely suggest having some sort of "loading" indicator while the table is empty and waiting for http response.
In terms of your question about the "data source", the data source of a UITableView is simply an object that implements the UITableViewDataSource protocol which you can read about here. Often times, you will have XCode set up a UITableViewController object which will act as both delegate and data source to your table view. How you actually store your data is up to you. The data source protocol simply provides the methods by which a table view will "ask" for the data it needs to load.

Using KVO to reload data in UITableView

I've been expanding my horizons recently and am trying to start utilizing KVO more in my programming.
I have a view controller in my iPhone application which acts as the datasource and delegate for a UITableView. I also have a singleton model controller which coordinates populating my model with data fetched from the web.
In my view controller, I request that the model's controller load new data from the web. Then I can observe the "dataset" property of the singleton and receive KVO notifications when items are added to or removed from the set.
Now, each cell in my table view has an indicator which specifies whether the content in that cell has been read or not (like the blue "unread" dot in mail). Like mail, when a row is selected, I'll display details about that row. In the viewDidLoad for the detail view, I set the object's "read" property to YES. I would like the original view controller to be able to observe this "read" property of each object in the dataset, so that [tableView reloadData] can automatically be called as necessary and redraw the cells without the blue dot.
In researching this, I found the following link: http://homepage.mac.com/mmalc/CocoaExamples/controllers.html#observingACollection
According to this, it looks like I will do the following:
1) Be an observer of the array
2) Whenever I get a notification of a change to the array, I add (or remove) myself as an observer for the individual properties I am interested in.
3) When I get a notification of a change to the property I'm interested in, I can call [tableView reloadData]
I'm currently in the process of attempting to implement this approach. Can anyone with experience doing this offer some advice on this approach? If this the best way to handle this type of situation?
If this is the correct approach, would anyone be willing to share their implementation of adding/removing the observers for objects in the collection when the collection changes?
Thanks!
I think you can accomplish this task by using Core Data and the Fetched Results Controller.
I'm sure this can save you a lot of work.
Here's a good guide: Ray Wenderlich Core Data Tutorial, getting started

Keeping data in a UIViewController, even after go to a TablevViewController and come back?

I have a UIViewController with some UITextFields in it which holds username and other details. And i need to go to another view, which is a UITableView which holds the list of countries to user to choose and get back to the UIViewController again, when user picks one.
What my problem is i will lose all my data mentioned previously in the UIViewControl, when get back from UITableView. I can use application's delegate to store data when go to UITableView and load them back when UIViewController is loaded again.
I was wondering, whether is there a way to achieve my goal, other than this approach? I have to use number of variables in the delegate to store these data as there are many.
Can anyone help me out with an idea please???
I dont think.But I think you are reloading the table in viewWillAppear.Thats why ur older values are not remaining

Adding and updating table view simultaneously

I have an array whose contents are updated and new entries are added on every seconds. How can i show the contents of an array in a table view so that i can see the newly inserted rows and updation of the added row.Can anyone help me.
Thanks in advance!!!
The answer suggested; [theTableView reload] definitely works fine. If you update the table every view seconds, however, your users will hate you.
Try to capture the reloading data in a notification handler. In that handler, check if the updated data belongs somewhere between the currently visible cells, if so, update the currently visible view. If not, ignore the updated data (but do add it to your underlying model). If the user scrolls further, after your update, cellForIndexPath: is called and the updated data will be drawn automatically.
reload is quite heavy to do every view seconds, certainly with a lot of data. drawing might get screwed up or worse..
You need to do a reload every time your datasource is updated. So if you table view is theTableView you would execute this instruction after your array is updated:
[theTableView reload]