使用 UISearchDisplayController 进行搜索 - 点击 tableview 单元格时出错
Search using UISearchDisplayController - error when tapping tableview cell
我创建了一个视图,通过使用 PFQueryTableViewController 和 UISearchDisplayController 来显示 Parse 服务器上用户 class 的所有用户,以便进行过滤和搜索。所有这些功能都运行良好,但仍然存在一个问题,即当我点击一个表格视图单元格以转到其用户详细信息视图时,我收到了这个错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
这是我的代码:
- (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];
}
}
任何人都可以帮助指出我的代码有什么问题或我应该研究哪里吗?我已经做了一些搜索,发现大多数人都使用故事板和 segue 技术,但我我没有使用故事板。非常感谢。
我假设错误在这一行:
PFUser *user = [userArray objectAtIndex:0];
UserArray
是一个空数组,您正在尝试访问第一个不存在的元素。您需要检查您是否 userArray
至少有一个元素。所以你的代码最好看起来像这样:
NSArray *userArray = [query findObjects];
if ([userArray count] == 0 {
//do nothing
return;
}
PFUser *user = [userArray objectAtIndex:0];
编辑:
您不应该为每个单元格使用 TapRecognizer 来识别它何时被点击。有一个 UITableViewDelegate
(https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html) 方法:tableView:didSelectRowAtIndexPath:
。在您的 ViewController
中实施此方法并丢弃手势识别器。
如果您希望仅当用户准确点击该标签时才发生操作,UILabel
仍然是错误的解决方案。最好使用 UIButton
.
我创建了一个视图,通过使用 PFQueryTableViewController 和 UISearchDisplayController 来显示 Parse 服务器上用户 class 的所有用户,以便进行过滤和搜索。所有这些功能都运行良好,但仍然存在一个问题,即当我点击一个表格视图单元格以转到其用户详细信息视图时,我收到了这个错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
这是我的代码:
- (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];
}
}
任何人都可以帮助指出我的代码有什么问题或我应该研究哪里吗?我已经做了一些搜索,发现大多数人都使用故事板和 segue 技术,但我我没有使用故事板。非常感谢。
我假设错误在这一行:
PFUser *user = [userArray objectAtIndex:0];
UserArray
是一个空数组,您正在尝试访问第一个不存在的元素。您需要检查您是否 userArray
至少有一个元素。所以你的代码最好看起来像这样:
NSArray *userArray = [query findObjects];
if ([userArray count] == 0 {
//do nothing
return;
}
PFUser *user = [userArray objectAtIndex:0];
编辑:
您不应该为每个单元格使用 TapRecognizer 来识别它何时被点击。有一个 UITableViewDelegate
(https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html) 方法:tableView:didSelectRowAtIndexPath:
。在您的 ViewController
中实施此方法并丢弃手势识别器。
如果您希望仅当用户准确点击该标签时才发生操作,UILabel
仍然是错误的解决方案。最好使用 UIButton
.