Navigating Through tvOS UITableView By Skipping Through Letters

I have a UITableView that lists the files located in the app alphabetically. As there are over 1500 files, I'm trying to implement functionality to navigate by letter of the alphabet when the right button is clicked on the remote. The issue I'm having is that the TableView indeed moves to the right spot, but instead of being highlighted like it normally would, I get a lighter highlighted color, and clicking select does nothing. If I then navigate normally by pressing down, it moves it back up to the 2nd item in the list.

```- (void)viewDidLoad {
    [super viewDidLoad];``
// We're setting the stage!
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.definesPresentationContext = YES;
self.currentIndex = 0;

// Time to call the stars for a rehearsal.
[self populateTheView];

// Let's make room for our magical focus director.
self.dapperFocusGuide = [[UIFocusGuide alloc] init];
[self.view addLayoutGuide:self.dapperFocusGuide];

// Red carpet time!
[self.dapperFocusGuide.topAnchor constraintEqualToAnchor:self.tableView.topAnchor].active = YES;
[self.dapperFocusGuide.leftAnchor constraintEqualToAnchor:self.tableView.leftAnchor].active = YES;
[self.dapperFocusGuide.widthAnchor constraintEqualToAnchor:self.tableView.widthAnchor].active = YES;
[self.dapperFocusGuide.heightAnchor constraintEqualToAnchor:self.tableView.heightAnchor].active = YES;
}- (void)populateTheView {
    NSBundle *bundle = [NSBundle mainBundle];
    self.title = @"Devo Songs";
    self.files  = [bundle pathsForResourcesOfType:@"pdf" inDirectory:@"WorshipSongs"];
    NSString *documentsDirectoryPath = [self.files objectAtIndex:self.currentIndex];
    
    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 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self becomeFirstResponder];

    // Initial spotlight position.
    self.dapperFocusGuide.preferredFocusEnvironments = @[self.tableView.visibleCells.firstObject];
}

- (BOOL)canBecomeFirstResponder {
    return YES; 
}


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

        
           UITableViewCell *cellToFocus = [self.tableView cellForRowAtIndexPath:indexPathToSelect];
           self.dapperFocusGuide.preferredFocusEnvironments = @[cellToFocus];
    } 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];

         
           UITableViewCell *cellToFocus = [self.tableView cellForRowAtIndexPath:indexPathToSelect];
           self.dapperFocusGuide.preferredFocusEnvironments = @[cellToFocus];
    }
}
  • https://www.youtube.com/watch?v=WWzh50TbW64

Add a Comment