2013-12-11 76 views
0

在我的主頁上,我創建了「卡片」,顯示保存到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]); 
    } 

}]; 

回答

0

在我看來,你認爲Objective-C的for/in語法會爲你進行類型檢查,但不幸的是它不會。你將一堆不是UIViews的PFObjects添加到自我卡片中,然後你多次覆蓋你的self.cardView並將它添加到自我卡片(大概是一個UIView子類)。然後你問每個對象的self.cards,並將它們全部添加到self.view的子視圖中,無論它們是否可以作爲子視圖存在。 (PFObjects不能作爲一個UIView的子視圖)

您的代碼:

for (UIView * view in self.cards) { 
    // do something with view 
} 

會因爲您使用的self.cards列表存儲基本上得到運行作爲

for (id item in self.cards) { 
    // blindly assume it's a UIView 
    UIView *view = (UIView *)item; 
    // do something with item that's been cast into a UIView 
    // since it's not a UIView, *crash right away* 
} 

PFObjects和self.cardView都會在這裏崩潰。我首先確保只有cardViews被添加爲self.view的子視圖。

此外,它看起來像你一次又一次地重寫self.cardView。在不瞭解你想做什麼的情況下,爲什麼不每次通過第一個for循環創建一個獨特的cardView,並將其添加到cardViews數組中,然後嘗試將所有這些cardviews添加到self.view中?