使用NSUserDefault保存的NSMutableArray填充UITableView

时间:2017-07-18 09:01:15

标签: ios objective-c uitableview

在我的应用程序中,我有一个UITextView,当我通过Alert Controller插入新数据时,它会在运行时填充。要打印此使用数据:

NSArray * textfields = alertController.textFields;
                
UITextField * urlfield = textfields [0];
UITextField * titlefield = textfields [1];
UITextField * categoryfield = textfields [2];
                
FeedInfo * newFeed = [[FeedInfo alloc] init];
NewFeed.feedURL = urlfield.text;
NewFeed.feedTitle = titlefield.text;
NewFeed.feedCategory = categoryfield.text;
                
[Self.feedArray addObject: newFeed];
[Self.tableView reloadData];

其中feedArray是已定义的类模型:

@interface FeedInfo : NSObject
@property (nonatomic,strong) NSString *feedURL;
@property (nonatomic,strong) NSString *feedTitle;
@property (nonatomic,strong) NSString *feedCategory;
@end

接下来,我使用:

打印IUTableView内的feedArray数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
FeedInfo *feedToShow = [self.feedArray objectAtIndex:indexPath.row];
cell.textLabel.text = feedToShow.feedTitle;
cell.detailTextLabel.text = feedToShow.feedCategory;
return cell;
  

行。到目前为止一切正常。

现在的问题是,在我退出应用程序之前,我想保存UITableView中的所有单元格,因为重新打开时仍必须包含所有插入的数据。

我想通过在[self.feedArray addObject: newFeed];之后和[self.tableView reloadData];

之前插入这段代码来保存所有feedArra数组(本主题的第1类)
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults
[UserDefaults setObject: self.feedArray forKey: @"feedArray"];
[userDefaults synchronize];

在viewDidLoad方法中:

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
Array = [userDefaults objectForKey: @ "feedArray"];

在cellForRowAtIndexPath中:

FeedInfo *feedToShow = [array objectAtIndex:indexPath.row];
cell.textLabel.text = feedToShow.feedTitle;
cell.detailTextLabel.text = feedToShow.feedCategory;

但它抛出Exception

 RSSReader[47834:2470261] [User Defaults] Attempt to set a
 non-property-list object ("<FeedInfo: 0x600000235200>" ) as an NSUserDefaults/CFPreferences value for key feedArray 2017-07-18 11:00:13.652
 RSSReader[47834:2470261] *** Terminating app due to uncaught exception
 'NSInvalidArgumentException', reason: 'Attempt to insert non-property
 list object (
     "<FeedInfo: 0x600000235200>" ) for key feedArray'

3 个答案:

答案 0 :(得分:1)

您的模型(Feed)应确认@interface FeedInfo : NSObject <NSCoding> @property (nonatomic,strong) NSString *feedURL; @property (nonatomic,strong) NSString *feedTitle; @property (nonatomic,strong) NSString *feedCategory; @end 以存档和取消存档数据。

<强> FeedInfo.h

@implementation FeedInfo

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    if (!self) {
        return nil;
    }

    self.feedURL = [aDecoder decodeObjectForKey:@"feed_url"];
    self.feedTitle = [aDecoder decodeObjectForKey:@"feed_title"];
    self.feedCategory = [aDecoder decodeObjectForKey:@"feed_category"];

    return self;

}

- (void)encodeWithCoder:(NSCoder *)coder {

    [coder encodeObject:_feedURL forKey:@"feed_url"];
    [coder encodeObject:_feedTitle forKey:@"feed_title"];
    [coder encodeObject:_feedCategory forKey:@"feed_category"];
}

@end

<强> FeedInfo.m

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.feedArray];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"feedArray"];

存储到Userdefault

NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"feedArray"];
NSArray *feedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

从UserDefault接收

    @interface AppDelegate ()
    @property (strong, nonatomic) UINavigationController *navigationController;
    @end    

//In Your Appdelegate didfinishlaunching method:

     self.window = [[UIWindow alloc] init];
     [self.window makeKeyAndVisible];
     self.navigationController = [[UINavigationController alloc] initWithRootViewController: YourViewController];
     self.window.rootViewController = self.navigationController;


     //In Your View controller:
     [self.navigationController pushViewController:controller animated:YES];

答案 1 :(得分:0)

NSUserDefault 支持这些类:NSNumber(Integer,Float,Double),NSString,NSDate,NSArray,NSDictionary,BOOL;

FeedInfo 是一个自定义类,您无法将其置于 NSUserDefault ;

您应该将 FeedInfo对象转换为 NSData ,然后添加它;

NSData * newData  = [NSKeyedArchiver archivedDataWithRootObject:newFeed];
[Self.feedArray addObject: newData];

答案 2 :(得分:0)

你可以使用这段代码。我的代码可以帮到你

NSMutableArray保存在NSUserDefaults

- (void)saveList:(NSMutableArray *)arr
{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:ZIPCODE_LIST];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

NSMutableArray

获取NSUserDefaults
- (NSMutableArray *)getList
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:ZIPCODE_LIST];
    NSMutableArray *arr = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: data];
    return arr;
}