0

I have tried following code so far:

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];

}

In this code I am retrieving an array of PFUsers as "objects".When I am saving the objects array to users it returns as empty. So can anyone please suggest me something that How can I set the username and profilepic in a tableView?

Log output of objects

Community
  • 1
  • 1
Sushrita
  • 725
  • 10
  • 29
  • can you log and check that you receive a valid array `objects` in `findObjectsInBackgroundWithBlock` – Vivek Molkar May 28 '15 at 05:13
  • @VivekMolkar yeah I can receive the array in 'objects' but cannot load it in 'users'. – Sushrita May 28 '15 at 05:14
  • can you show me the values in `objects` – Vivek Molkar May 28 '15 at 05:17
  • @VivekMolkar please see the question I have updated it with log output. – Sushrita May 28 '15 at 05:22
  • Just a check, you use `users = (NSArray*)[object ObjectForKey:@"User"];` make sure the key you used is right? coz image u just now uploaded shows `Users` but in the code it is `User` – Vivek Molkar May 28 '15 at 05:26
  • @VivekMolkar Actually I am not sure which key to use at 'objectForKey:' . can you please suggest me something that which key I should use there to load the array of 'users'. – Sushrita May 28 '15 at 05:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78970/discussion-between-sushrita-and-vivek-molkar). – Sushrita May 28 '15 at 05:31

2 Answers2

0

I guess query is not correct. Try this:

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];
    }

}];

In your code you're asking 'User' objects, then you fetching 'User' field from this objects.

Zhanserik Kenes
  • 335
  • 2
  • 7
0
- (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;
}
Sushrita
  • 725
  • 10
  • 29