I have an array of dictionaries loaded from a plist (below) called arrayHistory.
<plist version="1.0">
<array>
<dict>
<key>item</key>
<string>1</string>
<key>result</key>
<string>8.1</string>
<key>date</key>
<date>2009-12-15T19:36:59Z</date>
</dict>
...
</array>
</plist>
I filter this array based on ‘item’ so that a second array, arrayHistoryDetail has the same structure as arrayHistory but only contains e.g. ‘item’s equal to ‘1’. These detail items are successfully displayed in a tableView.
I then want to select an item from the tableView, and delete it from the tableView data source, arrayHistoryDetail (line 2 in the code below) – works, then I want to delete the item from the tableView itself (line 3 in the code below) – also works.
My problem is that I also need to delete it from the original arrayHistory, so I tried the following: created a temporary dictionary as an ivar:
NSMutableDictionary *tempDict;
@property (nonatomic, retain) NSMutableDictionary *tempDict;
Then my thinking was to make a copy in line 1 and remove it from the original array in line 4.
1 tempDict = [arrayHistoryDetail objectAtIndex: indexPath.row];
2 [arrayHistoryDetail removeObjectAtIndex: indexPath.row];
3 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
4 [arrayHistory removeObject:tempDict];
Didn’t work. Could someone please guide me in the right direction. I’m thinking that tempDict is a pointer and that removeObject needs a copy? I don’t know.
Thanks.
A couple of things to try:
First, make sure that the array you load from the plist file is mutable, i.e.:
Next, I wouldn’t use an ivar with a property just for a temporary variable. Your code could instead look something like this, and avoid the ivar/property for tempDict:
Third, be sure that the code you use to generate arrayHistoryDetail is not making a copy of the data in arrayHistory when it performs the filtering. If it does copy of the items, then tempDict will only hold a copy of the dictionary you want to delete, which means it will have a different pointer value than the actual dictionary in arrayHistory. The removeObject method requires an exact pointer to the object you intend to delete.
If the copying does turn out to be a problem, you may have change your data structure a little. The best option would be to use assign a unique identifier to each dictionary within arrayHistory, and use that information to determine which dictionary to delete.