简体   繁体   English

调用[tableView reloadData]时,TableView无法重新加载

[英]TableView not reloading when calling [tableView reloadData]

I'm trying to get my tableView (initial viewController) to refresh when I select a row from another tableView in a container. 当我从容器中的另一个tableView中选择一行时,我试图刷新tableView(初始viewController)。

When the row is selected, it parses a JSON file from a web server, saves it and store it in the docs directory. 选中该行后,它将解析来自Web服务器的JSON文件,将其保存并存储在docs目录中。 The tableView from the main view should be reloading with the new JSON file. 主视图中的tableView应该重新加载新的JSON文件。

This is how it should be working: 这是应该如何工作的:

在此处输入图片说明

OpenWorkViewController.m 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 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 (parts have been omitted, bc they are useless) ViewController.m (部分已被省略,因为它们没有用)

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

What is wrong guys?! 伙计们怎么了? Tell me, and I'll be forever in debt. 告诉我,我将永远负债累累。

Thanks! 谢谢!

One thing jumps out to me in a quick read through the code. 快速阅读代码可以使我想到一件事。

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

I assume that getDataInBackgroundWithBlock is asynchronous, which means that when you get to the last three lines there, jsonData may not actually contain any information. 我假设getDataInBackgroundWithBlock是异步的,这意味着当您到达那里的最后三行时,jsonData实际上可能不包含任何信息。

You should add some code in the block to save the data, and then make sure that you only load the new vc after you have finished loading everything. 您应该在块中添加一些代码以保存数据,然后确保仅在完成所有加载后才加载新的vc。

It is probably because you are presenting the view controller on the background thread. 可能是因为您要在后台线程上显示视图控制器。 Try presenting it on the main run loop: 尝试将其呈现在主运行循环上:

[self.jsonData writeToFile:appFile atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
    ViewController *vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
    [vc viewDidLoad];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM