开发者

Deleting rows UItableview iphone using userdefaults

开发者 https://www.devze.com 2023-03-26 13:39 出处:网络
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 s

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 exa开发者_如何学运维mple, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号