首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 7上的UITableView节索引间距

iOS 7上的UITableView节索引间距
EN

Stack Overflow用户
提问于 2013-09-21 02:42:56
回答 7查看 15K关注 0票数 26

在iOS 6上,我的UITableView的分区索引将在表视图的高度上均匀地分布它的所有项。在iOS 7中,所有的项目都集中在中间,这使得这些项目很难点击。有没有办法把它们隔开?

EN

回答 7

Stack Overflow用户

发布于 2013-11-18 08:19:22

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

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

票数 7
EN

Stack Overflow用户

发布于 2014-01-04 01:21:25

这是Anton Malmygin answer的一个实现,你可以通过在数组上添加更多的空iten来添加更多的空间:

首先,让我们创建一个带有伪索引的数组。

代码语言:javascript
复制
    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委托方法:

代码语言:javascript
复制
- (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;
}

结果如下:

票数 7
EN

Stack Overflow用户

发布于 2014-02-19 03:10:44

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

因此,首先,您将这些元素添加到viewDidLoad或其他形式的indexTitles数组中。注意:如果您正在向tableView异步添加内容,这也可以动态完成,这就是我添加if语句的原因,所以只要它不是第一个语句,它就会用句点替换空字符串。这是为了在结束时不会有流失期。

代码语言:javascript
复制
@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:@""];
}

现在我们可以只使用模运算符,这取决于我们每个索引标题有多少个字符串。

代码语言:javascript
复制
- (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声明部分索引标题。

代码语言:javascript
复制
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.indexTitles;
}

下面是我制作的一个动态加载内容的示例:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18923729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档