I have created a view to display all users from user class on Parse server by using PFQueryTableViewController also with UISearchDisplayController in order to do filtering and searching. All of those features is working fine but there remains one problem that is when I tap a tableview cell to go to its user detail view I got this error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PFUser *user = (tableView == self.tableView) ? self.objects[indexPath.row] : self.searchResults[indexPath.row];
static NSString *identifier = @"reuseIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
// display user name
NSString *userName = [NSString stringWithFormat:@"@%@", user.username];
cell.textLabel.text = userName;
cell.textLabel.textColor = [UIColor darkGrayColor];
// tap user ????
UITapGestureRecognizer * tap = [UITapGestureRecognizer new];
[tap addTarget:self action:@selector(handleNameTap:)];
cell.textLabel.userInteractionEnabled = YES;
[cell.textLabel addGestureRecognizer:tap];
//--????
return cell;
}
- (void)handleNameTap:(UIGestureRecognizer *)tap
{
CGPoint touchLocation = [tap locationOfTouch:0 inView:self.tableView];
NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:touchLocation];
PAWOtherUserProfileViewController *otherProfileViewController = [[PAWOtherUserProfileViewController alloc] initWithNibName:nil bundle:nil];
PAWUserProfileViewController *userProfileViewController = [[PAWUserProfileViewController alloc] initWithNibName:nil bundle:nil];
PFQuery *query = [PFUser query];
[query whereKey:kPAWParseUsernameKey equalTo:[self.searchResults objectAtIndex:tappedRow.row]];
NSArray *userArray = [query findObjects];
PFUser *user = [userArray objectAtIndex:0];
if ([user.username isEqualToString:[PFUser currentUser].username]) {
NSLog(@"current user");
userProfileViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentViewController:userProfileViewController animated:YES completion:nil];
} else {
NSLog(@"other user");
otherProfileViewController.userPassed = user;
otherProfileViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentViewController:otherProfileViewController animated:YES completion:nil];
}
}
Could anyone help point out what's wrong with my code or where I should look into? I have done some search about it and found most people use storyboard with segue technique but I'm not using storyboard. Thanks very much.