在 Table 视图显示之前未加载解析数据

Parse data not loading before Table View shows

我正在使用 Parse 将数据提取到 Table View。我正在使用 PFQueryTableViewController.

当我 运行 应用程序时,一些 Parse 数据加载到 Table View 中,有些则没有。所以我必须拉动刷新一次才能全部加载。

我尝试将 [self.tableView reloadData] 放在几个不同的地方,尝试将我的代码移动到 viewDidLoad viewDidAppear viewWillAppear 等等,但无法正常工作.

当 运行s 时,我看到我所有的数据都正确地记录在控制台中。

我设置断点来查看首先加载的内容,它按以下顺序进行: initWithCoder -> viewDidLoad -> (PFQuery *)queryForTable

我确定我完全错过了一些简单的东西,但我想不通,有什么想法吗?谢谢!

我有很多代码,请告诉我哪些部分会有所帮助,我会尽快 post。

编辑:根据 soulshined 的请求添加代码

- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        // The className to query on
        self.parseClassName = @"na";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"match";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;
    }

    return self;
}

- (PFQuery *)queryForTable {
    // GMT Date from Phone
    NSDate *gmtNow = [NSDate date];

    // Query Parse
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query orderByAscending:@"dateGame"];
    [query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];

    return query;
}

编辑 - 根据 Yuchen 添加

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *playoffsIdentifier = @"PlayoffsCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:playoffsIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:playoffsIdentifier];
    }

    // Matchup
    UILabel *matchLabel = (UILabel*) [cell viewWithTag:101];
    matchupLabel.text = [object objectForKey:@"match"];

    // Date
    UILabel *dateLabel = (UILabel*) [cell viewWithTag:102];
    dateLabel.text = [object objectForKey:@"date"];

    // Time
    UILabel *timeLabel = (UILabel*) [cell viewWithTag:103];
    timeLabel.text = [object objectForKey:@"time"];

    // Color
    // Using App Group - Because wasn't taking global color array for some reason
    NSString *container = @"group.com.thwams.play";
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
    NSArray *colorGroup = [defaults objectForKey:@"KeyColor"];
    NSLog(@"ColorCell: %@", colorGroup);
    cell.backgroundColor = [self colorWithHexString:[colorGroup objectAtIndex:indexPath.row]];

    return cell;
}

// Added to convert Hex colors to RGB
-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor grayColor];
    if ([cString hasPrefix:@"("]) return [UIColor colorWithRed:7.0f/255.0f green:32.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString length] != 6) return  [UIColor grayColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Hide Nav Bar
    [self.navigationController setNavigationBarHidden:YES];

    // GMT Date from Phone
    NSDate *gmtNow = [NSDate date];
    NSLog(@"GMT Now: %@", gmtNow);

    // Query Parse
    PFQuery *query = [self queryForTable];
    [query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSMutableArray *localMatchup = [@[] mutableCopy];
            NSMutableArray *localDate = [@[] mutableCopy];
            NSMutableArray *localTime = [@[] mutableCopy];
            NSMutableArray *localTV = [@[] mutableCopy];
            NSMutableArray *localColor = [@[] mutableCopy];

            for (PFObject *object in objects) {
                // Add objects to local Arrays
                [localMatchup addObject:[object objectForKey:@"matchup"]];
                [localDate addObject:[object objectForKey:@"date"]];
                [localTime addObject:[object objectForKey:@"time"]];
                [localTV addObject:[object objectForKey:@"tv"]];
                [localColor addObject:[object objectForKey:@"color"]];

                // App Group
                NSString *container = @"group.com.thwams.playoffs";
                NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

                // Matchup
                [defaults setObject:localMatchup forKey:@"KeyMatchup"];
                NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
                NSLog(@"Default Matchup: %@", savedMatchup);
                savedMatchup = matchupArray;

                // Date
                [defaults setObject:localDate forKey:@"KeyDate"];
                NSArray *savedDate = [defaults objectForKey:@"KeyDate"];
                NSLog(@"Default Date: %@", savedDate);
                savedDate = dateArray;

                // Time
                [defaults setObject:localTime forKey:@"KeyTime"];
                NSArray *savedTime = [defaults objectForKey:@"KeyTime"];
                NSLog(@"Default Time: %@", savedTime);
                savedTime = timeArray;

                // TV
                [defaults setObject:localTV forKey:@"KeyTV"];
                NSArray *savedTV = [defaults objectForKey:@"KeyTV"];
                NSLog(@"Default TV: %@", savedTV);
                savedTV = tvArray;

                // Color
                [defaults setObject:localColor forKey:@"KeyColor"];
                NSArray *savedColor = [defaults objectForKey:@"KeyColor"];
                NSLog(@"Default Color: %@", savedColor);
                savedColor = colorArray;
            }

        }
    }];
}

这是您的代码中的问题。以下代码在后台 运行。因此,完成后您将需要一个[tableview reload]。您将需要在主线程中执行此操作。

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
     // a lot of stuff going on here
}

所以修复看起来像下面这样:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
     if (!error) {
        // your code ... 
        for (PFObject *object in objects) {
             // your code ...
        }

        // Add these here
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reload];
        });  
     }
}