0

I'm using Parse to pull data into a Table View. I'm using PFQueryTableViewController.

When I run the app, some of the Parse data loads in the Table View and some doesn't. So I have to pull to refresh once for it to all be loaded.

I've tried putting [self.tableView reloadData] a few different places, tried moving my code to viewDidLoad viewDidAppear viewWillAppear and such, and can't get anything working.

I see all my data logging correctly in the console when it's runs.

I set breakpoints to see what was loading first and it went in this order: initWithCoder -> viewDidLoad -> (PFQuery *)queryForTable

I'm sure I'm totally missing something easy, but I can't figure it out, any ideas? Thanks!

I've got tons of code so just let me know what parts would be helpful, and I'll post ASAP.

EDIT: Adding code per request of 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;
}

EDIT - adding per 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;
            }

        }
    }];
}
14
  • Show the initWithCoder and queryForTable please and thanks. The fact that it's not all loading to begin with is an issue itself Commented Feb 21, 2015 at 22:16
  • Where do you NSLog you data? could you show relevant code there? What happens if you call [self.tableview reloaddata] right after logging? do you have your designed data shown in the table view? Commented Feb 21, 2015 at 22:44
  • @soulshined I added the two you requested, let me know if anything else needs clarified. Any ideas? Commented Feb 22, 2015 at 19:57
  • You could try to use NSNotification and observe for objects before updating tableview. Commented Feb 22, 2015 at 19:58
  • @YuchenZhong I NSLog my data at basically every point in the code. I've tried doing self.tableview reloaddata at almost every spot in my code and haven't seen anything happen. I'm not sure what you mean by "designed data", so let me know what you're getting at with that. If you have a specific method in my code you'd like to see, just let me know and I'll post immediately. Thanks! Commented Feb 22, 2015 at 20:00

1 Answer 1

2

Here is the problem in your code. The following code is running in background. Therefore, you will need a [tableview reload] after it is done. And you will need to do this in main thread.

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

So the fix looks like the following:

[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];
        });  
     }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.