在UITableView中,在部分索引标题之间添加填充(或增加高度)

3
我已经通过sectionIndexTitlesForTableView:sectionForSectionIndexTitle:方法为我的UITableView实现了一个章节索引。我只有几个章节,而且默认情况下它们在屏幕上垂直居中,每个索引标题之间的空间非常小。我在其他应用程序中看到过他们增加索引之间的空间量,不是很大但至少增加了一些间隙,提高了用户点击所需的准确性。我想知道如何实现这一点?
这正是我想要的效果-请注意右侧索引之间的额外空间: enter image description here
1个回答

2
你可以按照这个答案中提供的方法添加额外的空格。

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

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://dev59.com/p2Mk5IYBdhLWcg3w2BTJ

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://dev59.com/p2Mk5IYBdhLWcg3w2BTJ
    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://dev59.com/p2Mk5IYBdhLWcg3w2BTJ
    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
    }

- (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://dev59.com/p2Mk5IYBdhLWcg3w2BTJ
    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://dev59.com/p2Mk5IYBdhLWcg3w2BTJ
    if ([sectionsTitle[section] isEqualToString:@""]){
        return 0;
    }
    return // your logic here;
}


希望这能有所帮助。


谢谢,这正是我想要的。好技巧 - 没想到要添加空字符串索引。那个链接的问题有我在谷歌上搜索后找到的完全相同的截图 - 真是讽刺。 - Jordan H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接