【问题标题】:Add padding (or increase height) between section index titles in UITableView在 UITableView 中的部分索引标题之间添加填充(或增加高度)
【发布时间】:2014-06-20 14:41:19
【问题描述】:

我已经通过sectionIndexTitlesForTableView:sectionForSectionIndexTitle: 方法为我的UITableView 实现了一个部分索引。我只有几个部分,默认情况下它们在屏幕上垂直居中,每个索引标题之间的空间很小。我在其他应用程序中看到它们增加了索引之间的空间量,不是显着但至少几个点给他们一些喘息的空间,并在用户尝试点击他们想要的那个时提高准确性。我想知道如何才能做到这一点?

这正是我想要获得的 - 请注意右侧索引之间的额外空间:

【问题讨论】:

    标签: ios objective-c uitableview height padding


    【解决方案1】:

    您可以做的是添加此answer 中建议的额外空格。

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

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


    希望它会有所帮助。

    【讨论】:

    • 谢谢,这正是我所追求的。好技巧 - 没想到要添加空字符串索引。链接到的那个问题有我在谷歌搜索后找到的确切截图 - 具有讽刺意味。
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多