我不知道你的具体错误;可能是您没有(id)sender
在方法中添加 a 。我想出了如何做到这一点。我将在下面添加我的代码:
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (NSArray *)keyCommands {
return @[[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:0 action:@selector(moveDownOneRow:) discoverabilityTitle:@"Select row down"],
[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:0 action:@selector(moveUpOneRow:) discoverabilityTitle:@"Select row up"]];
}
- (void)moveDownOneRow:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (indexPath == nil) {
indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
} else {
if ((indexPath.row + 1) < [self.tableView numberOfRowsInSection:0]) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
}
}
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)moveUpOneRow:(id)sender {
NSIndexPath *indexPath = [self.tableViewindexPathForSelectedRow];
if (indexPath == nil) {
indexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:0]-1) inSection:0];
} else {
if ((indexPath.row - 1) >= 0) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
}
}
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}