为什么 NSMutableArray 不保存视图已更改的数据?
Why NSMutableArray doesn't save the data one the view has changed?
我有一个表viewcontroller(停靠站),其中显示了停靠站列表 (stopsArray),用户可以在其中将特定的停靠站标记为他的收藏夹。触摸 UIImage 后,停靠点将保存在 NSMutableArray (myStopsMArray) 中。直到这里一切正常。但是在我离开 viewcontroller 的那一刻,我丢失了 NSMutableArray 的所有数据。这是我的代码:
Stops.h:
#import <UIKit/UIKit.h>
@interface Stops : UIViewController <UITableViewDataSource, UITableViewDelegate>;
@property (weak, nonatomic) IBOutlet UITableView *stopsTable;
@property (nonatomic, retain) NSArray *stopsArray;
@property (nonatomic, retain) NSMutableArray *myStopsMArray;
@end
Stops.m:
#import "Stops.h"
#import "Stop.h"
#import "MyStops.h"
#import "Global.h"
@interface Stops ()
@end
@implementation Stops
@synthesize stopsArray, myStopsMArray, stopsTable;
- (void)viewWillAppear:(BOOL)animated
{
if (!self.myStopsMArray) {
self.myStopsMArray = [[NSMutableArray alloc] init];
}
}
- (void) addToFavourites:(id)sender {
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSInteger tag = gesture.view.tag;
NSIndexPath *tagIndex = [NSIndexPath indexPathForItem:tag inSection:0];
UITableViewCell *cell = [stopsTable cellForRowAtIndexPath:tagIndex];
if ([cell.imageView.image isEqual: [UIImage imageNamed:@"star_filled_white.png"]]) {
// Add Stop to favourites
[self.myStopsMArray addObject:[stopsArray objectAtIndex:tag]];
cell.imageView.image = [UIImage imageNamed:@"star_filled_yellow.png"];
} else {
// Delete Stops from favourites
[self.myStopsMArray removeObject:[stopsArray objectAtIndex:tag]];
cell.imageView.image = [UIImage imageNamed:@"star_filled_white.png"];
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.myStopsMArray forKey:@"myStopsMArray"];
[userDefaults synchronize];
NSArray *savedData = [userDefaults objectForKey:@"myStopsMArray"];
NSLog(@"%@", savedData);
}
@end
非常感谢。
如果有人和我在同一个地方,这就是神奇的线-已经更正了-:
Stops.m:
if ([savedData containsObject: [stopsArray objectAtIndex:index]]) {
...
}
不要忘记之前声明 NSUserDefaults:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *savedData = [userDefaults objectForKey:@"myStopsMArray"];
我有一个表viewcontroller(停靠站),其中显示了停靠站列表 (stopsArray),用户可以在其中将特定的停靠站标记为他的收藏夹。触摸 UIImage 后,停靠点将保存在 NSMutableArray (myStopsMArray) 中。直到这里一切正常。但是在我离开 viewcontroller 的那一刻,我丢失了 NSMutableArray 的所有数据。这是我的代码:
Stops.h:
#import <UIKit/UIKit.h>
@interface Stops : UIViewController <UITableViewDataSource, UITableViewDelegate>;
@property (weak, nonatomic) IBOutlet UITableView *stopsTable;
@property (nonatomic, retain) NSArray *stopsArray;
@property (nonatomic, retain) NSMutableArray *myStopsMArray;
@end
Stops.m:
#import "Stops.h"
#import "Stop.h"
#import "MyStops.h"
#import "Global.h"
@interface Stops ()
@end
@implementation Stops
@synthesize stopsArray, myStopsMArray, stopsTable;
- (void)viewWillAppear:(BOOL)animated
{
if (!self.myStopsMArray) {
self.myStopsMArray = [[NSMutableArray alloc] init];
}
}
- (void) addToFavourites:(id)sender {
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSInteger tag = gesture.view.tag;
NSIndexPath *tagIndex = [NSIndexPath indexPathForItem:tag inSection:0];
UITableViewCell *cell = [stopsTable cellForRowAtIndexPath:tagIndex];
if ([cell.imageView.image isEqual: [UIImage imageNamed:@"star_filled_white.png"]]) {
// Add Stop to favourites
[self.myStopsMArray addObject:[stopsArray objectAtIndex:tag]];
cell.imageView.image = [UIImage imageNamed:@"star_filled_yellow.png"];
} else {
// Delete Stops from favourites
[self.myStopsMArray removeObject:[stopsArray objectAtIndex:tag]];
cell.imageView.image = [UIImage imageNamed:@"star_filled_white.png"];
}
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.myStopsMArray forKey:@"myStopsMArray"];
[userDefaults synchronize];
NSArray *savedData = [userDefaults objectForKey:@"myStopsMArray"];
NSLog(@"%@", savedData);
}
@end
非常感谢。
如果有人和我在同一个地方,这就是神奇的线-已经更正了-:
Stops.m:
if ([savedData containsObject: [stopsArray objectAtIndex:index]]) {
...
}
不要忘记之前声明 NSUserDefaults:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *savedData = [userDefaults objectForKey:@"myStopsMArray"];