gpt4 book ai didi

ios - 在 UITableView 中显示来自 Parse.com 上两个类的(全部)数据

转载 作者:行者123 更新时间:2023-11-29 01:20:36 26 4
gpt4 key购买 nike

我希望能够显示来自 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)

最佳答案

我将尝试总结如何将自己从 PFQueryTableViewController(和 UITableViewController)中解放出来。如果两者都不是,世界会变得更快乐一些这些类已经被发明了(imo))。创建一个名为 ViewController 的 UIViewController 子类。

在 IB 中,添加一个 UIViewController(将其类设置为 ViewController),为其提供一个限制在 View 边缘的 UITableView。连接数据源、委托(delegate)和一个名为 tableView 的 socket 。添加原型(prototype)单元格。现在,只需使用标准副标题或左侧细节 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);
});
});

关于ios - 在 UITableView 中显示来自 Parse.com 上两个类的(全部)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com