当我从容器中的另一个tableView中选择一行时,我试图让我的tableView(初始viewController)刷新。
选择行后,它会从Web服务器解析JSON文件,保存并将其存储在docs目录中。主视图中的tableView应该使用新的JSON文件重新加载。
这是应该如何运作的:
OpenWorkViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath{
PFQuery *query = [PFQuery queryWithClassName:@"Travaux"];
[query whereKey:@"profID" equalTo:@"EAPFfaGSOE"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"form.json"];
for (PFObject *object in objects) {
PFFile *file = object[@"json"];
[file getDataInBackgroundWithBlock:^(NSData *jsonData, NSError *error) {
if (!error) {
self.jsonData = jsonData;
}
}];
}
[self.jsonData writeToFile:appFile atomically:YES];
ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
[vc viewDidLoad];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];}
的 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *form;
}
@property CGFloat shortQuestion;
@property CGFloat choiceQuestion;
@property CGFloat multipleQuestion;
@property (strong, nonatomic) NSMutableArray *numbersCell;
@property (strong, nonatomic) NSMutableArray *questionType;
@property (strong, nonatomic) NSMutableArray *inputsArray;
@property (strong, nonatomic) NSMutableDictionary *inputsDict;
@property (strong, nonatomic) NSString *indexAnswer;
@property (strong, nonatomic) NSString *formData;
-(void)readQuestions:(NSString *)path;
@end
ViewController.m (部分已被省略,bc它们没用)
- (void)viewDidLoad {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"form.json"];
//NSLog(@"%s === %@", __PRETTY_FUNCTION__, filePath);
self.formData = filePath;
[self readQuestions:filePath];
UIView *shortView = [[[NSBundle mainBundle] loadNibNamed:@"ECFormShortQuestion" owner:self options:nil] objectAtIndex:0];
self.shortQuestion = [shortView bounds].size.height;
UIView *choiceView = [[[NSBundle mainBundle] loadNibNamed:@"ECFormChoiceQuestion" owner:self options:nil] objectAtIndex:0];
self.choiceQuestion = [choiceView bounds].size.height;
UIView *multipleView = [[[NSBundle mainBundle] loadNibNamed:@"ECFormMultipleQuestion" owner:self options:nil] objectAtIndex:0];
self.multipleQuestion = [multipleView bounds].size.height;
self.automaticallyAdjustsScrollViewInsets = NO; //important
[super viewDidLoad];}
-(void)readQuestions:(NSString *)path
{
//NSString *filePath =[[NSBundle mainBundle] pathForResource:@"input" ofType:@"json"];
self.numbersCell = [[NSMutableArray alloc] init];
self.questionType = [[NSMutableArray alloc] init];
self.inputsArray = [[NSMutableArray alloc] init];
self.inputsDict = [[NSMutableDictionary alloc] init];
if ([path length])
{
NSError *readError;
NSData *questionsData = [[NSData alloc] initWithContentsOfFile:path options:NSMappedRead error:&readError];
if (!readError)
{
NSError *parseError;
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:questionsData options:NSJSONReadingMutableContainers error:&parseError];
if (!parseError && dictionary)
{
NSMutableDictionary *questionsDictionary = [dictionary objectForKey:@"questions"];
int number= -1; // Question number
//NSLog(@"%@",questionsDictionary);
for (NSMutableDictionary *singleQuestion in questionsDictionary)
{
NSString *questionType = [singleQuestion objectForKey:@"type"];
NSString *questionTitle = [singleQuestion objectForKey:@"title"];
NSMutableDictionary *inputs = [singleQuestion objectForKey:@"inputs"];
NSMutableArray *a = [NSMutableArray array];
for (NSDictionary *d in inputs)
{
NSArray *arr = [d allKeys];
NSString *theKey = [arr lastObject];
[a addObject:theKey];
}
self.indexAnswer = [NSString stringWithFormat:@"%d", ++number];
for (int i = 1;i<=[a count];i++)
{
[self.inputsDict setObject:a forKey:self.indexAnswer];
}
[self.numbersCell addObject:questionTitle];
[self.questionType addObject:questionType];
}
}
}
form=[[UITableView alloc]init];
form.frame = CGRectMake(0,64,700,704);
form.dataSource=self;
form.delegate=self;
form.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:form];
[form reloadData];
}}
有什么不对的家伙?!告诉我,我将永远负债。
谢谢!
答案 0 :(得分:1)
通过快速阅读代码,有一件事情向我跳了出来。
for (PFObject *object in objects) {
PFFile *file = object[@"json"];
[file getDataInBackgroundWithBlock:^(NSData *jsonData, NSError *error) {
if (!error) {
self.jsonData = jsonData;
}
}];
}
[self.jsonData writeToFile:appFile atomically:YES];
ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
[vc viewDidLoad];
我假设getDataInBackgroundWithBlock是异步的,这意味着当你到达最后三行时,jsonData可能实际上不包含任何信息。
您应该在块中添加一些代码来保存数据,然后确保在加载完所有内容后才加载新的vc。
答案 1 :(得分:1)
可能是因为您在后台线程上呈现了视图控制器。尝试在主运行循环中显示它:
"foo".freeze.object_id
=> 87323600
foo = "foo".freeze
foo.object_id
=> 87323600