iOS 7上的UITableView部分索引间距

Sen*_*ior 25 iphone uitableview ios ios7

在iOS 6上,我的UITableView的节索引将在表视图的高度均匀分布其所有项目.在iOS 7中,所有项目都在中间聚集在一起,使得项目难以点击.有没有办法将它们分开?

Ant*_*gin 7

这里可能的解决方案之一是在每次向段索引标题数组添加实际标题之后添加空字符串,并在您的tableView: sectionForSectionIndexTitle: atIndex:方法中返回索引除以2 .

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


Gui*_*tro 7

这是Anton Malmygin答案的一个实现,您可以通过在阵列上添加更多空的itens来添加更多空间:

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

    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:@""];
    }
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下方法实现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;
}
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述


Jat*_*tin 3

我找到了一个解决方法:

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

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

 [self setIndexUIForTableView:tableview];

    [tableview reloadData];
Run Code Online (Sandbox Code Playgroud)