在我的主頁上,我創建了「卡片」,顯示保存到Parse的信息。刷新UIView與解析數據
用戶可以刷一張卡片以揭示它後面的新卡片。
這裏是我迄今爲止嘗試:
- (void)viewDidLoad
{
[super viewDidLoad];
[self displayProfileInfo];
// I am manually setting NUM_CARDS for now (this needs to be changed to the array of how many objects are in my Parse table though
self.cards = [[NSMutableArray alloc] initWithCapacity:NUM_CARDS];
for (int i = 0; i < NUM_CARDS; i++)
{
self.cardView = [[CardView alloc] initWithFrame:CGRectMake(self.view.center.x-150, self.view.center.y-160, 300, 360)];
self.cardView.front.layer.backgroundColor = [UIColor darkGrayColor].CGColor;
self.cardView.back.layer.backgroundColor = self.cardView.front.layer.backgroundColor;
[self.cards addObject:self.cardView];
}
for (UIView * view in self.cards)
{
[self.view addSubview:view];
}
self.originalPoint = self.view.center;
}
- (void)displayProfileInfo
{
PFQuery * query = [PFQuery queryWithClassName:@"Profile"];
[query orderByDescending:@"createdAt"]; [query findObjectsInBackgroundWithBlock:(NSArrayobjects, NSError error) {
if (error)
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else
{
for (int i = 0; i < objects.count; i++)
{
self.profileObject = [objects objectAtIndex:i];
PFFile * imageFile = [self.profileObject objectForKey:@"imageFile"];
NSURL * imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSData * imageData = [NSData dataWithContentsOfURL:imageFileURL];
self.cardView.profilePicture.image = [UIImage imageWithData:imageData];
self.cardView.nameLabel.text = [self.profileObject objectForKey:@"fullName"];
self.cardView.locationLabel.text = [self.profileObject objectForKey:@"location"];
}
}
}]; }
我致電viewDidLoad中此方法,並在我的表中的第一對象顯示在第一張卡片。
當我刷卡時,下一張卡片不會加載任何數據。
在下一個可用信息中加載卡片的最佳方式是什麼?
編輯:
PFQuery * query = [PFQuery queryWithClassName:@"Profile"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.cards = [[NSMutableArray alloc] initWithArray:objects];
for (int i = 0; i < objects.count; i++)
{
self.cardView = [[CardView alloc] initWithFrame:CGRectMake(self.view.center.x-150, self.view.center.y-160, 300, 360)];
self.cardView.front.layer.backgroundColor = [UIColor darkGrayColor].CGColor;
self.cardView.back.layer.backgroundColor = self.cardView.front.layer.backgroundColor;
[self.cards addObject:self.cardView];
}
for (UIView * view in self.cards)
{
// **** CRASHES HERE ****
// -[PFObject superview]: unrecognized selector sent to instance is what I get
[self.view addSubview:view];
}
if (error)
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else
{
for (int i = 0; i < objects.count; i++)
{
self.profileObject = [objects objectAtIndex:i];
PFFile * imageFile = [self.profileObject objectForKey:@"imageFile"];
NSURL * imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSData * imageData = [NSData dataWithContentsOfURL:imageFileURL];
self.cardView.profilePicture.image = [UIImage imageWithData:imageData];
self.cardView.nameLabel.text = [self.profileObject objectForKey:@"fullName"];
self.cardView.locationLabel.text = [self.profileObject objectForKey:@"location"];
}
}
}];
這裏是新的代碼,這裏是我在控制檯中看到:
- [PFObject上海華]:無法識別的選擇發送到實例...
所以這還需要解決:
PFQuery * query = [PFQuery queryWithClassName:@"Profile"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.cards = [[NSMutableArray alloc] initWithArray:objects];
for (int i = 0; i < objects.count; i++)
{
self.cardView = [[CardView alloc] initWithFrame:CGRectMake(self.view.center.x-150, self.view.center.y-160, 300, 360)];
self.cardView.front.layer.backgroundColor = [UIColor darkGrayColor].CGColor;
self.cardView.back.layer.backgroundColor = self.cardView.front.layer.backgroundColor;
self.profileObject = [objects objectAtIndex:i];
PFFile * imageFile = [self.profileObject objectForKey:@"imageFile"];
NSURL * imageFileURL = [[NSURL alloc] initWithString:imageFile.url];
NSData * imageData = [NSData dataWithContentsOfURL:imageFileURL];
self.cardView.profilePicture.image = [UIImage imageWithData:imageData];
self.cardView.nameLabel.text = [self.profileObject objectForKey:@"fullName"];
self.cardView.locationLabel.text = [self.profileObject objectForKey:@"location"];
[self.cards addObject:self.cardView];
[self.view addSubview:self.cardView];
}
if (error)
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];