我正在使用Parse将数据提取到Table View
。我正在使用PFQueryTableViewController
。
当我运行该应用时,Parse
中的一些Table View
数据会加载,有些则不会。所以我必须刷新一次才能加载所有内容。
我已尝试将[self.tableView reloadData]
放在几个不同的位置,尝试将我的代码移至viewDidLoad
viewDidAppear
viewWillAppear
等等,并且无法正常工作。< / p>
我看到所有数据在运行时都在控制台中正确记录。
我设置断点以查看首先加载的内容并按顺序执行:
initWithCoder
- &gt; viewDidLoad
- &gt; (PFQuery *)queryForTable
我确信我完全错过了一些简单的东西,但我无法弄清楚,有什么想法吗?谢谢!
我有很多代码,所以请让我知道哪些部分会有所帮助,我会尽快发布。
编辑:根据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;
}
}
}];
}
答案 0 :(得分:2)
以下是您的代码中的问题。以下代码在后台运行。因此,完成后您需要[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];
});
}
}