2015-10-05 100 views
0

我試圖讓我的tableView(初始viewController)刷新時,我從容器中的另一個tableView中選擇一行。TableView不調用時調用[tableView reloadData]

選中該行後,它將解析來自Web服務器的JSON文件,將其保存並存儲在docs目錄中。應該使用新的JSON文件重新加載主視圖中的tableView。

這是它應該如何工作的:

enter image description here

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]; 
}} 

什麼是錯的傢伙?告訴我,我會永遠欠債。

謝謝!

回答

1

這可能是因爲您在後臺線程上呈現視圖控制器。嘗試在主運行循環中呈現它:

[self.jsonData writeToFile:appFile atomically:YES]; 
dispatch_async(dispatch_get_main_queue(), ^{ 
    ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil]; 
    [vc viewDidLoad]; 
}); 
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。

+0

我要試試這個馬上,我會向您介紹一個更新!謝謝。 –

相關問題