显示(全部)UITableView中Parse.com上两个类的数据

时间:2016-01-07 01:33:32

标签: ios objective-c uitableview parse-platform

我希望能够显示来自Parse.com的两个类的所有数据,但它无法正常工作。每次我打开tableview时,应用程序都会崩溃。这是我的代码。我使用https://www.parse.com/questions/query-with-two-classes的指导来帮助我。

- (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)

1 个答案:

答案 0 :(得分: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);
    });
});