如何在iOS的UITableVIew中显示解析的PFUser数组?
到目前为止,我已经尝试了以下代码:如何在iOS的UITableVIew中显示解析的PFUser数组?,ios,objective-c,uitableview,pfuser,parse-framework,Ios,Objective C,Uitableview,Pfuser,Parse Framework,到目前为止,我已经尝试了以下代码: PFGeoPoint * myGeoPoint = [PFUser currentUser][@"coordinates"]; // Your geoPoint PFQuery *query = [PFUser query]; [query includeKey:@"User"]; [query whereKey:@"coordinates" nearGeoPoint:myGeoPoint withi
PFGeoPoint * myGeoPoint = [PFUser currentUser][@"coordinates"]; // Your geoPoint
PFQuery *query = [PFUser query];
[query includeKey:@"User"];
[query whereKey:@"coordinates" nearGeoPoint:myGeoPoint withinMiles:radiusInMiles];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
for (PFUser *object in objects){
users = (NSArray*)[object ObjectForKey:@"User"];
}
[self.tableView reloadData];
}
}
}];
- (PFUser*) objectAtIndexPath:(NSIndexPath*)indexPath {
PFUser *object = [PFUser objectWithClassName: @"User"];
[object setObject:[users objectAtIndex:indexPath.row] forKey:@"User"];
return object;
}
// Use myObjects for numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return users.count;
}
//Use your custom object for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath object:(PFUser *)object {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell using object
UILabel *reviewLabel = (UILabel *)[cell viewWithTag:10];
reviewLabel.text = [object objectForKey:object.username];
PFFile *userImageFile = object[@"profilePic"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
if (imageData != nil)
cell.imageView.image = [UIImage imageWithData:imageData];
else cell.imageView.image= [UIImage imageNamed:@"defaultPerson"];
}
}];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SMChatViewController *chatController = [SMChatViewController alloc];
// [chatController initWithPhoto:image];
[self presentModalViewController:chatController animated:YES];
}
在这段代码中,我将PFUsers数组作为“objects”检索。当我将objects数组保存给用户时,它返回为空。那么,有谁能给我一些建议,我如何在tableView中设置用户名和profilepic
我猜这个查询是不正确的。 试试这个:
PFGeoPoint * myGeoPoint = [PFUser currentUser][@"coordinates"]; // Your geoPoint
PFQuery *query = [PFQuery queryWithObject:@"_User"];
NSArray *userList = [NSArray new];
[query whereKey:@"coordinates" nearGeoPoint:myGeoPoint withinMiles:radiusInMiles];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error){
userList = objects;
[self.tableView reloadData];
}
}];
在您的代码中,您询问“用户”对象,然后从该对象获取“用户”字段 你能在
findobjectsinbackgroundithblock
@VivekMolkar中记录并检查你是否收到了一个有效的数组objects
。是的,我可以在“objects”中接收数组,但不能在“users”中加载它。你能给我看看objects
@VivekMolkar中的值吗?请看问题,我用日志输出更新了它。只是检查一下,您使用users=(NSArray*)[objectobjectforkey:@“User”]代码>确保您使用的钥匙是正确的?因为刚才上传的图像u显示的是用户
,但在代码中它是用户
查询是正确的,但我无法将数组加载到tableView,您能帮我吗?您能帮我建议如何将数组加载到tableView吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
PFUser *user = (PFUser*)[users objectAtIndex:indexPath.row];
cell.textLabel.text=user.username;
PFFile *userImageFile = user[@"profilePic"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
if (imageData != nil)
cell.imageView.image = [UIImage imageWithData:imageData];
else cell.imageView.image= [UIImage imageNamed:@"defaultPerson.png"];
}
}];
return cell;
}