[英]How to save tableview rows to a file and reload to tableview
I have a simple tableview and I populated it with a text.field :我有一个简单的 tableview,我用 text.field 填充它:
@interface ListaDeProdutosTableViewController ()
@property NSMutableArray *produtos;
@end
@implementation ListaDeProdutosTableViewController
Now I want to save this rows to a file (to persist data) and read it back to tableview.现在我想将这些行保存到一个文件中(以保留数据)并将其读回 tableview。 How can I do this?
我怎样才能做到这一点?
Can you help me?你能帮助我吗? Thank you!
谢谢!
Not sure what you want to save, but here's an example how to save object to file and read it later:不确定要保存什么,但这里有一个示例,如何将对象保存到文件并稍后读取:
- (NSString *)cachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
- (void)cacheObject:(NSString *)objectId withData:(SomeClass *)objectData{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objectData];
NSString *path = [NSString stringWithFormat:@"%@/someobject-%@.cache", [self cachePath], objectId];
[data writeToFile:path atomically:YES];
}
- (SomeClass *)getObjectCache:(NSString *)objectId {
NSString *path = [NSString stringWithFormat:@"%@/someobject-%@.cache", [self cachePath], objectId];
NSData *data = [NSData dataWithContentsOfFile:path];
if (!data) {
return nil;
}
SomeClass *object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return object;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.