简体   繁体   中英

Deleting rows UItableview iphone using userdefaults

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM