从UITableView中删除没有行的部分

Bot*_*Bot 8 objective-c uitableview ios5

如果该部分没有行,我想从UITableView中删除节标题.

我正在使用UILocalizedIndexedCollation我的节标题.所以当我创建标题时,我不一定知道哪些部分会有内容.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    //return [customerSections count];
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return 1;
    }
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"Section: %i", section);
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return self.filteredCustomers.count;
    } else {
        return [[self.customerData objectAtIndex:section] count];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // The header for the section is the region name -- get this from the region at the section index.

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return nil;//@"Results";
    }
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //return [customerSections allKeys];
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return nil;
    }
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Run Code Online (Sandbox Code Playgroud)

owe*_*rig 10

只是想插入并解决这个问题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if ([self.myTableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
        return nil;
    }
    return [[self.collation sectionTitles] objectAtIndex:section];

}
Run Code Online (Sandbox Code Playgroud)

根据这个答案


Bot*_*Bot 1

我最终删除了未使用的sectionIndexTitles并基于此创建了部分。

在我的中NSURLConnection requestDidFinish我使用了以下内容。

self.customerData = [self partitionObjects:[self customers] collationStringSelector:@selector(self)];
Run Code Online (Sandbox Code Playgroud)

然后有

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    sectionIndexTitles = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[collation sectionTitles] count];
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];

    for (int i = 0; i < sectionCount; i++) {
        [unsortedSections addObject:[NSMutableArray array]];
    }

    for (id object in array) {
        NSInteger index = [collation sectionForObject:[object objectForKey:@"name"] collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }

    NSMutableArray *sections = [NSMutableArray array];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

    NSUInteger lastIndex = 0;
    NSMutableIndexSet *sectionsToRemove = [NSMutableIndexSet indexSet];
    for (NSArray *section in unsortedSections) {
        if ([section count] == 0) {
            NSRange range = NSMakeRange(lastIndex, [unsortedSections count] - lastIndex);
            [sectionsToRemove addIndex:[unsortedSections indexOfObject:section inRange:range]];
            lastIndex = [sectionsToRemove lastIndex] + 1;

        } else {
            NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors];
            [sections addObject:sortedArray];
        }
    }

    [sectionIndexTitles removeObjectsAtIndexes:sectionsToRemove];

    return sections;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.

    if (self.searchDisplayController.active) {
        return 1;
    }
    return [sectionIndexTitles count];//[[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (self.searchDisplayController.active) {
        return self.filteredCustomers.count;
    } else {
        return [[self.customerData objectAtIndex:section] count];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (self.searchDisplayController.active) {
        return nil;
    }

    return [sectionIndexTitles objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{

    if (self.searchDisplayController.active) {
        return nil;
    }

    return sectionIndexTitles;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;//[[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Run Code Online (Sandbox Code Playgroud)

对于上述所有代码,这删除了屏幕右侧未使用的字母以及没有行的节标题。