【问题标题】:UITableView section index spacing on iOS 7iOS 7 上的 UITableView 部分索引间距
【发布时间】:2013-09-26 06:03:46
【问题描述】:

在 iOS 6 上,我的 UITableView 的部分索引将其所有项目均匀地分布在表格视图的高度上。在 iOS 7 中,所有项目都聚集在中间,使得项目难以点击。有什么办法可以把它们分开吗?

【问题讨论】:

标签: iphone ios uitableview ios7


【解决方案1】:

这是我玩过和工作的东西。它遵循 Anton M 的建议,使用 Swift 2 和 ios 9。

假设您有以下部分索引:

let indexes: [String] = ["A", "B", "C", "D", "E"]

所以每个人都在同一个页面上,A 在索引 0 处。B 在索引 1 处,依此类推。

假设您希望索引之间有两个空格。然后将您的部分索引定义为:

let indexes: [String] = ["A", "", "", "B", "", "", "C", "", "", "D", "", "", "E"]

所以现在,A 仍然在索引 0,但 B 现在在索引 3,C 在 6,D 在 9,E 在 12。

您只需要计算sectionForSectionIndexTitle 中的偏移量。您所追求的是真实标题的索引,因为这是您在表中拥有的。因此:

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {        
        return index / 3
}

因此,对于索引之间需要的每个额外空间,您需要将 1 添加到上述表达式的枚举数。您可以在 Playground 中轻松进行测试。

【讨论】:

    【解决方案2】:

    这是Anton Malmygin答案的实现,您可以通过在数组上添加更多空元素来添加更多空间:

    首先,让我们用假索引创建一个数组。

        NSArray *array = self.mydataArray; // here are your true index
        self.sectionsTitle = [NSMutableArray array];
        int n = array.count;
    
        // In IOS 7 all index of the items are clumped together in the middle,
        // making the items difficult to tap.
        // As workaround we added "fake" sections index
        // reference: https://stackoverflow.com/questions/18923729/uitableview-section-index-spacing-on-ios-7
    
        for (int i = 0; i < n; i++){
            [self.sectionsTitle  addObject:array[i]];
            [self.sectionsTitle  addObject:@""];
        }
    

    然后,您可以使用以下方法实现 tableview 委托方法:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        // In IOS 7 all index of the items are clumped together in the middle,
        // making the items difficult to tap.
        // As workaround we added "fake" sections index
        // reference: https://stackoverflow.com/questions/18923729/uitableview-section-index-spacing-on-ios-7
        if ([sectionsTitle[section] isEqualToString:@""]){
            return 0;
        }
        return x; // return your desire section height 
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        // In IOS 7 all index of the items are clumped together in the middle,
        // making the items difficult to tap.
        // As workaround we added "fake" sections index
        // reference: https://stackoverflow.com/questions/18923729/uitableview-section-index-spacing-on-ios-7
        if ([sectionsTitle[section] isEqualToString:@""]){
            return nil;
        }else{
           // return your desire header view here, 
           // if you are using the default section header view, 
           // you don't need to implement this method
           return // return your custom view
        }
    }
    
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return self.sectionsTitle;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        // In IOS 7 all index of the items are clumped together in the middle,
        // making the items difficult to tap.
        // As workaround we added "fake" sections index
        // reference: https://stackoverflow.com/questions/18923729/uitableview-section-index-spacing-on-ios-7
        if ([title isEqualToString:@""]){
             return -1;
        }
        return [sectionsTitle indexOfObject:title];
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // In IOS 7 all index of the items are clumped together in the middle,
        // making the items difficult to tap.
        // As workaround we added "fake" sections index
        // reference: https://stackoverflow.com/questions/18923729/uitableview-section-index-spacing-on-ios-7
        if ([sectionsTitle[section] isEqualToString:@""]){
            return 0;
        }
        return // your logic here;
    }
    

    结果如下:

    【讨论】:

      【解决方案3】:

      有一种更简单的方法可以完成 Anton 的建议,它也可以扩展到每个部分的任意数量的索引(而不是像 Anton 建议的那样只有两个)。因此,您可以添加所需的额外索引标题的数量。在这个例子中,我使用了 4。

      所以首先你将这些东西添加到indexTitles 数组、viewDidLoad 或其他任何地方。 注意:如果您向 tableView 添加内容,这也可以动态完成 异步,这就是我添加 if 语句的原因,只要它不是第一个 一个它将用句点替换空字符串。这样就不会有流浪 结束时的句号

      @property (nonatomic, strong) NSArray *indexTitles;
      
      
      - (void) addIndexTitle:(NSString *) indexTitle {
              if ([self.indexTitles count]) {
                  NSInteger last = self.indexTitles.count;
                  self.indexTitles[last - 1] = @".";
                  self.indexTitles[last - 2] = @"."; // Change the number of these depending
                  self.indexTitles[last - 3] = @"."; // on the number of lines in the index titles
              }
              [self.indexTitles addObject:indexTitle];
              [self.indexTitles addObject:@""];  // Add different strings here for multiline 
              [self.indexTitles addObject:@""];  // index titles
              [self.indexTitles addObject:@""];
      }
      

      现在我们可以根据每个索引标题有多少个字符串来使用模运算符。

      - (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
          index = index - index % 4;  // This is the index of the section you want
          NSInteger newIndex = index;
          // do any other processing here (if need be) to determine the correct index here
          return newIndex;
      

      }

      不要忘记为 tableView 声明节索引标题。

      - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
          return self.indexTitles;
      }
      

      这是我制作的动态加载内容的一个:

      【讨论】:

        【解决方案4】:

        这里可能的解决方案之一是在每次将真实标题添加到您的部分索引标题数组并在您的 tableView: sectionForSectionIndexTitle: atIndex: 方法中返回除以 2 的索引之后添加空字符串。

        这个技巧稍微增加了连续标题之间的距离,但并不能完全解决这个问题。

        【讨论】:

        • 这是一个愚蠢的 hack,但如果你能预测你想要显示的章节标题的数量,这是一个简单的修复并且效果很好。不知道为什么苹果会选择这样改变这个控件!
        • 我只是通过@Anton Malmygin 解决方案的实现来创建答案。
        【解决方案5】:

        我尝试了 Jatin 的解决方法,但结果只是部分索引的颜色和字体发生了变化。它保持垂直压缩和居中。

        我认为这是 iOS7 的新行为(虽然我认为这不是最好的视觉外观)。此行为在 Apple 的官方文档中也可见:iOS Human Interface Guidelines - Table View

        【讨论】:

          【解决方案6】:

          我尝试了这个模块并且它可以工作,但是你还必须在 viewDidAppear 方法中设置调用例程,所以每次视图出现时你都需要调用这个例程然后重新加载你的表。

          【讨论】:

            【解决方案7】:

            我找到了解决方法:

            - (void)setIndexUIForTableView:(UITableView*)tableView{
                //UI the index view
                for(UIView *view in [tableView subviews]) {
                    if([view respondsToSelector:@selector(setIndexColor:)]) {
                        [view performSelector:@selector(setIndexColor:) withObject:[UIColor clientblack]];
            
                        if ([view respondsToSelector:@selector(setFont:)]) {
                            [view performSelector:@selector(setFont:) withObject:[UIFont systemFontOfSize:15.0]];
                            [view performSelector:@selector(setBackgroundColor:) withObject:[UIColor clientsidebarGray]];
            
                        }
                    }
                }
            }
            

            将以下内容添加到您的方法中并在加载数据后

             [self setIndexUIForTableView:tableview];
            
                [tableview reloadData];
            

            【讨论】:

            • 你用这个来定位标签了吗?
            • 我用它来增加字体大小并通过重新加载两次来正确定位标签.....一次在 cellforrowatindexpath 中,一次在数据到达时
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多