在UITableView中的节索引标题之间添加填充(或增加高度)

时间:2014-05-04 05:54:29

标签: ios objective-c uitableview height padding

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

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

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以做的是添加此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;
}
希望它会有所帮助。