【问题标题】:Remove sections with no rows from UITableView从 UITableView 中删除没有行的部分
【发布时间】:2012-03-26 03:44:16
【问题描述】:

如果该部分没有行,我想从 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];
}

【问题讨论】:

    标签: objective-c uitableview ios5


    【解决方案1】:

    只是想插话并给出我的解决方案

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

    based on this answer

    【讨论】:

    • 虽然我最终也删除了 sectionIndexTitles,但这比上述答案更好地回答了这个问题。
    【解决方案2】:

    我最近用这段代码实现了这一点:

    这是返回部分标题的函数,如果该部分中没有行,则不返回标题:

      - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
         if ([self.customerData count] > 0 ) {
                 return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
            }
        return nil;
    }
    

    【讨论】:

      【解决方案3】:

      这是一个有趣的问题,有许多可能的解决方案。

      向后工作,numberOfSectionsinTableViewnumberOfRowsInSection 需要更新才能显示正确数量的部分。这些部分依赖于方法UILocalizedIndexedCollation 方法。

      (这可能发生在某种用户操作(删除或插入)之后,因此请注意在commitEditingStyle 中您应该调用[self.tableView reloadData)。

      我假设 customerData 是一个数组,其中每个索引都有一个对应于一个部分的可变数组。当某个 customerData 索引处的数组没有数据时,您希望删除该索引的部分。

      然后,解决方案是手动计算所有内容 - 根据 customerData 数组中的生命来确定部分信息。 我尝试重写你的三个方法。祝你好运!

      - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
      {
          if (tableView == self.searchDisplayController.searchResultsTableView) {
              return nil;
          }
         NSMutableArray *arrayToFilter = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
      
         //This is the key - recalculate index titles based on what's present in customerData
         for (int i = [self.customerData count] -1; i >=0 ; i--) {
             if (![[self.customerData objectAtIndex:i] count]) {
                  [self.arrayToFilter removeObjectAtIndex:i];
             }
         }
         return arrayToFilter;
      }
      
      //You need to be calculating your table properties based on the number of objects
      //rather of the 'alphabet' being used.
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
          // Return the number of sections.
          //return [customerSections count];
          if (tableView == self.searchDisplayController.searchResultsTableView) {
              return 1;
          }
          //return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
          int populatedArrayCounter = 0;        
          for (int i = 0; i <[self.customerData count]; i++) {
              if ([[self.customerData objectAtIndex:i] count]) {
                  populatedArrayCounter++;
              }
          }
          return populatedArrayCounter;
      }
      
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          //NSLog(@"Section: %i", section);
          if (tableView == self.searchDisplayController.searchResultsTableView) {
              return self.filteredCustomers.count;
          } else {
              // Your original line requires changing, because there are customerData array objects with a count of 0, and you don't want any sections like that
              // Thus, pick from the set of populated arrays.
             NSMutableArray populatedArrays = [[NSMutableArray alloc] init];        
             for (int i = 0; i <[self.customerData count]; i++) {
                 if ([[self.customerData objectAtIndex:i] count]) {
                     [populatedArrays addObject:i];
                 }
             }
             return [[populatedArrays objectAtIndex:section] count];;
          }
      }
      

      【讨论】:

        【解决方案4】:

        我想快速分享我的解决方案

        func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            if self.sections[section].isEmpty
            {
                return nil
            }
            else
            {
                return collation.sectionTitles[section]
            }
        }
        

        【讨论】:

          【解决方案5】:

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

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

          self.customerData = [self partitionObjects:[self customers] collationStringSelector:@selector(self)];
          

          然后有

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

          使用上述所有代码,这会从屏幕右侧删除未使用的字母以及没有行的部分标题。

          【讨论】: