Ios 在表视图显示之前未加载分析数据
我正在使用Parse将数据拉入Ios 在表视图显示之前未加载分析数据,ios,objective-c,uitableview,parse-platform,Ios,Objective C,Uitableview,Parse Platform,我正在使用Parse将数据拉入表视图。我正在使用PFQueryTableViewController 当我运行应用程序时,一些Parse数据加载到表视图中,而一些则没有加载。因此,我必须拉动刷新一次,才能加载所有内容。 我试着把[self.tableView reloadData]放在几个不同的地方,试着把我的代码移到viewDidLoadviewdidappease等等,但什么都不能工作 当控制台运行时,我可以在控制台中正确地看到所有数据记录 我设置了断点以查看首先加载的是什么,并按照以下顺序
表视图
。我正在使用PFQueryTableViewController
当我运行应用程序时,一些Parse
数据加载到表视图中,而一些则没有加载。因此,我必须拉动刷新一次,才能加载所有内容。
我试着把[self.tableView reloadData]
放在几个不同的地方,试着把我的代码移到viewDidLoad
viewdidappease
等等,但什么都不能工作
当控制台运行时,我可以在控制台中正确地看到所有数据记录
我设置了断点以查看首先加载的是什么,并按照以下顺序进行:
initWithCoder
->viewDidLoad
->(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;
}
}
}];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象{
静态NSString*playoffsIdentifier=@“PlayoffsCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:playoffsIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:playoffsIdentifier];
}
//对决
UILabel*matchLabel=(UILabel*)[带标记的单元格视图:101];
matchupLabel.text=[objectobjectforkey:@“match”];
//日期
UILabel*dateLabel=(UILabel*)[带标记的单元格视图:102];
dateLabel.text=[objectobjectforkey:@“date”];
//时间
UILabel*timeLabel=(UILabel*)[带标记的单元格视图:103];
timeLabel.text=[objectobjectforkey:@“time”];
//颜色
//使用应用程序组-因为某些原因没有使用全局颜色数组
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]];
返回单元;
}
//添加以将十六进制颜色转换为RGB
-(UIColor*)带十六进制字符串的颜色:(NSString*)十六进制
{
NSString*cString=[[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]大写字符串];
//字符串应为6或8个字符
if([cString length]<6)返回[UIColor grayColor];
if([cString hasPrefix:@”(“])返回[UIColor color with red:7.0f/255.0f green:32.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
//如果出现,请删除0X
如果([cString hasPrefix:@“0X]”)cString=[cString substringFromIndex:2];
如果([cString length]!=6)返回[UIColor grayColor];
//分为r、g、b子串
NSRange范围;
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];
//扫描值
无符号整数r,g,b;
[[NSScanner scannerWithString:rsString]scanHexInt:&r];
[[NSScanner scannerWithString:gString]scanHexInt:&g];
[[NSScanner SCANNER WITHSTRING:bString]scanHexInt:&b];
返回[UIColor COLOR WITHRED:((浮动)r/255.0f)
绿色:((浮动)g/255.0f)
蓝色:((浮动)b/255.0f)
α:1.0f];
}
-(无效)viewDidLoad{
[超级视图下载];
//隐藏导航条
[self.navigationController设置NavigationBarHidden:是];
//GMT电话日期
NSDate*gmtNow=[NSDate日期];
NSLog(@“GMT现在:%@”,GMT现在);
//查询解析
PFQuery*query=[self queryForTable];
[查询whereKey:@“dateGame”大于或等于:gmtNow];
[查询findObjectsInBackgroundWithBlock:^(NSArray*对象,NSError*错误){
如果(!错误){
NSMutableArray*localMatchup=[@[]mutableCopy];
NSMutableArray*localDate=[@[]mutableCopy];
NSMutableArray*localTime=[@[]mutableCopy];
NSMutableArray*localTV=[@[]mutableCopy];
NSMutableArray*localColor=[@[]mutableCopy];
用于(PFObject*对象中的对象){
//将对象添加到本地数组
[localMatchup addObject:[object objectForKey:@“matchup”];
[localDate addObject:[object objectForKey:@“date”];
[localTime addObject:[objectobjectforkey:@“time”];
[localTV addObject:[object objectForKey:@“tv”];
[localColor addObject:[object objectForKey:@“color”];
//应用程序组
NSString*container=@“group.com.thwams.季后赛”;
NSUserDefaults*defaults=[[NSUserDefaults alloc]initWithSuiteName:container];
//对决
[默认设置对象:localMatchup forKey:@“KeyMatchup”];
NSArray*savedMatchup=[默认对象forkey:@“KeyMatchup]”;
NSLog(@“默认匹配:%@”,savedMatchup);
savedMatchup=matchupArray;
//日期
[默认设置setObject:localDate-forKey:@“KeyDate”];
NSArray*savedDate=[defaults objectForKey:@“KeyDate”];
NSLog(@“默认日期:%@”,savedDate);
savedDate=dateArray;
//时间
[默认设置对象:localTime forKey:@“KeyTime”];
NSArray*savedTime=[defaults objectForKey:@“KeyTime”];
NSLog(@“默认时间:%@”,保存时间);
savedTime=timeArray;
[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];
});
}
}