2016-01-07 24 views
0

我希望能夠顯示來自Parse.com的兩個類的所有數據,但它不起作用。每當我去打開桌面視圖的應用程序崩潰。這是我的代碼。我使用https://www.parse.com/questions/query-with-two-classes的指導來幫助我。顯示(全部)來自Parse.com上的兩個類的數據在UITableView中

- (id)initWithStyle:(UITableViewStyle)style { 
    self = [super initWithStyle:style]; 
    if (self) { 
     // This table displays items in the class 
     self.parseClassName = @"rep"; 
     self.parseClassName = @"rep2"; 

     self.pullToRefreshEnabled = YES; 
     self.paginationEnabled = NO; 
     self.objectsPerPage = 100; 
    } 
    return self; 
} 

- (PFQuery *)queryForTable { 

    PFQuery *1query = [PFQuery queryWithClassName:@"rep"]; 


    PFQuery *2query = [PFQuery queryWithClassName:@"rep2"]; 


    PFQuery *query = [PFQuery orQueryWithSubqueries:@[1query,2query]]; 
      [query whereKey:@"user" equalTo:[PFUser currentUser]]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) { 


    }]; 


    return query; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
         object:(PFObject *)object { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell to show todo item with a priority at the bottom 
    cell.textLabel.text = [object objectForKey:@"name"]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Username: %@", 
           [object objectForKey:@"username"]]; 

    return cell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return YES; 
} 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    // Remove the row from data model 
    PFObject *objectToDel = [self.objects objectAtIndex:indexPath.row]; 

    [objectToDel deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 



     UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"Item Was Deleted Successfully. Pull Down to Refresh Tab" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [Alert show]; 



    } 
    ]; 
} 


@end 
>  Specwatch[668:192464] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'All sub queries of an 
> `or` query should be on the same class.' 
>  *** First throw call stack: 
>  (0x182319900 0x181987f80 0x182319848 0x1000bd8b4 0x100025c10 0x100072514 0x100071e00 0x18700c0c0 0x1870cbda8 0x1870cbc80 
> 0x1870caec8 0x1870caa6c 0x1870ca694 0x1870ca5fc 0x187007778 
> 0x184a16b2c 0x184a11738 0x184a115f8 0x184a10c94 0x184a109dc 
> 0x184a0a0cc 0x1822d0588 0x1822ce32c 0x1822ce75c 0x1821fd680 
> 0x18370c088 0x187074d90 0x10006d054 0x181d9e8b8) 
>  libc++abi.dylib: terminating with uncaught exception of type NSException 
>  (lldb) 
+0

,請複製粘貼後墜毀的日誌,並表明它發生在哪一行。 parseClassName的行賦值中的兩個實際上就像第二行一樣。你的queryForTable方法也是不常見的,用OR來形成一個查詢,然後進一步限定它,然後運行它,然後返回它(所有不尋常的)。 – danh

+0

你應該回滾到日誌頂部,發佈關鍵信息。 – SeanChense

+0

我在原始問題中發佈了完整報告@SeanChense – spe

回答

1

我會試着總結一下如何去從PFQueryTableViewController(和UITableViewController解放自己對於這個問題,世界將是一個稍微更快樂的地方,如果沒有這些類的有被髮明(imo))。創建一個名爲ViewController的子類,名稱爲UIViewController

在IB中,添加一個UIViewController(將其類設置爲ViewController),給它一個約束爲視圖邊緣的UITableView。連接數據源,委託和一個名爲tableView的插座。添加一個原型單元格。現在,只需使用標準字幕或留下細節UITableViewCell。給單元格標識符爲@"cell"

// viewcontroller.m 
@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 

@property(weak,nonatomic) IBOutlet UITableView *tableView; 
@property(strong,nonatomic) NSMutableArray *objects; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [PFCloud callFunctionInBackground:@"getTwoClasses" withParameters:nil block:^(NSArray *objects, NSError *error) { 
     self.objects = objects; 
     [self.tableView reloadData]; 
    }]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.objects.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    PFObject *object = self.objects[indexPath.row]; 
    cell.textLabel.text = [object objectId]; 
    cell.detailTextLabel.text = [object parseClassName]; 
    return cell; 
} 

這個非常簡單的實現應該是客戶端需要的所有東西。在服務器上,我們只需要一個名爲「getTwoClasses」的雲功能來從兩個類(rep和rep2)中獲取對象。

部署這一功能做到這一點...

Parse.Cloud.define("getTwoClasses", function(request, response) { 
    var reps; 
    var user = request.user; 
    var repQuery = new Parse.Query("rep"); 
    repQuery.equalTo("user", user); 
    query.find().then(function(result) { 
     reps = result; 
     var rep2Query = new Parse.Query("rep2"); 
     rep2Query.equalTo("user", user); 
     return rep2Query.find(); 
    }).then(function(result) { 
     response.success(reps.concat(result)); 
    }, function(error) { 
     response.error(error); 
    }); 
}); 
+0

謝謝!有兩個語法錯誤引發了我的興趣,並且在repQuery.equalTo函數 – spe

+0

Oops中在「用戶」和用戶之間放置了一個逗號。抱歉。編輯。 – danh

+0

如果你覺得它是一個正確的答案,習慣上把它標爲正確。 – danh

相關問題