SectionIndexTitlesForTableView correctly aligned with sections

I have a table view with many sections, the name for these sections is just AZ and #, as in the application for the iPhone address book. I applied sectionIndexTitlesForTableView to quickly jump to a specific letter and basically just return an array of letters AZ and #.

This will work if my list always contains an element for each letter of the alphabet, but it will not, and this screws up the headings of the section indexes, because pressing C in the list can go to D if the 3rd section is D (i.e. if there is nothing in section C.

I know that I can only return an array to sectionIndexTitlesForTableView with letters that are sections, but that would look a little strange and would not be the same functionality as the iPhone address book application.

How can i fix this?

+3
source share
2 answers

Basically you should implement:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [sections indexOfObject:title];
}

and go back based on the index and name in which section it should be included. Where sections is an array storing a list of sections

+3
source

I don’t see how the @Rudiger method will work if you have only the headings of the indices A, C, F, S, T and the section for AZ. This situation may occur when using MPMediaQuery.

, : tableview , , , .

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSString *sectionTitle = nil;
    NSComparisonResult result;
    int i;

    for(i = 0; i < tableView.numberOfSections; i++)
    {
        sectionTitle = [self tableView:tableView titleForHeaderInSection:i];
        result = [title compare:sectionTitle];

        if(result != NSOrderedDescending)
            break;
    }

    return (MIN (i, (tableView.numberOfSections - 1)));
}

UPDATE

, , '.

+6

Source: https://habr.com/ru/post/1774876/


All Articles