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;
}
}
}];
}
NSLogyou 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?NSLogmy data at basically every point in the code. I've tried doingself.tableview reloaddataat 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!