我有一个 UITableView,它按字母顺序列出应用程序中的文件。由于有超过 1500 个文件,我试图实现在单击遥控器上的右键时按字母表字母导航的功能。我遇到的问题是 TableView 确实移动到了正确的位置,但我没有像通常那样突出显示,而是得到了更浅的突出显示颜色,并且单击选择什么也不做。如果我然后通过按下按钮正常导航,它会将其移回到列表中的第二项。我做错了什么?
-(void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
self.currentIndex = 0;
[self populateTheView];
}
-(void)populateTheView {
NSBundle *bundle = [NSBundle mainBundle];
self.title = @"Devo Songs";
self.files = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"WorshipSongs"];
NSString *documentsDirectoryPath = [self.files objectAtIndex:thepath.row];
self.filenames = [[documentsDirectoryPath lastPathComponent] stringByDeletingPathExtension];
NSMutableArray *names = [NSMutableArray arrayWithCapacity:[self.files count]];
for (NSString *path in self.files) {
[names addObject:[[path lastPathComponent] stringByDeletingPathExtension]];
}
//self.files = names;
self.files = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"FILESLOAD%@", self.files);
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder]; // Make the view controller the first responder to handle remote control events.
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
// Override pressesBegan method to detect right arrow key press event.
- (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
[super pressesEnded:presses withEvent:event];
for (UIPress *press in presses) {
if (press.type == UIPressTypeRightArrow) {
[self handleRightButtonPress];
}
}
}
- (void)handleRightButtonPress {
// Get the current name.
NSString *currentName = self.files[self.currentIndex];
// Get the current letter.
NSString *currentLetter = [currentName substringToIndex:1];
// Find the next index starting from the current index.
NSInteger nextIndex = self.currentIndex + 1;
while (nextIndex < self.files.count) {
NSString *nextName = self.files[nextIndex];
NSString *nextLetter = [nextName substringToIndex:1];
if (![nextLetter isEqualToString:currentLetter]) {
break;
}
nextIndex++;
}
// Check if we found a valid next index.
if (nextIndex < self.files.count) {
// Scroll the table view to the row corresponding to the next letter.
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:nextIndex inSection:0]
atScrollPosition:UITableViewScrollPositionTop animated:NO];
// Update the current index to the new index.
self.currentIndex = nextIndex;
// Deselect the current selected row (if any).
NSIndexPath *previousSelectedIndexPath = [self.tableView indexPathForSelectedRow];
if (previousSelectedIndexPath) {
[self.tableView deselectRowAtIndexPath:previousSelectedIndexPath animated:NO];
}
// Select the cell at the specified index.
NSIndexPath *indexPathToSelect = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
[self.tableView selectRowAtIndexPath:indexPathToSelect animated:NO scrollPosition:UITableViewScrollPositionNone];
} else {
// If we have reached the end of the list, reset the index to the first element.
self.currentIndex = 0;
// Scroll back to the top of the table view.
[self.tableView setContentOffset:CGPointZero animated:NO];
// Deselect the current selected row (if any).
NSIndexPath *previousSelectedIndexPath = [self.tableView indexPathForSelectedRow];
if (previousSelectedIndexPath) {
[self.tableView deselectRowAtIndexPath:previousSelectedIndexPath animated:NO];
}
// Select the cell at the specified index.
NSIndexPath *indexPathToSelect = [NSIndexPath indexPathForRow:self.currentIndex inSection:0];
[self.tableView selectRowAtIndexPath:indexPathToSelect animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}