Iphone iOS 7上的UITableView节索引间距
在iOS 6上,my UITableView的节索引将在整个表视图的高度上均匀分布其所有项。在iOS 7中,所有项目都集中在中间,使得项目难以挖掘。有没有办法把它们隔开?我找到了一个解决办法:Iphone iOS 7上的UITableView节索引间距,iphone,ios,uitableview,ios7,Iphone,Ios,Uitableview,Ios7,在iOS 6上,my UITableView的节索引将在整个表视图的高度上均匀分布其所有项。在iOS 7中,所有项目都集中在中间,使得项目难以挖掘。有没有办法把它们隔开?我找到了一个解决办法: - (void)setIndexUIForTableView:(UITableView*)tableView{ //UI the index view for(UIView *view in [tableView subviews]) { if([view responds
- (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]];
}
}
}
}
在加载数据后,将以下内容添加到您的方法中
[self setIndexUIForTableView:tableview];
[tableview reloadData];
我尝试了这个mod,它可以工作,但是你还必须在viewdideappease方法中设置调用例程,这样每次视图出现时,你都需要调用这个例程,然后重新加载你的表 我尝试了Jatin的解决方法,但结果只是部分索引的颜色和字体发生了变化。它保持垂直压缩和居中
我认为这是iOS7的新行为(尽管我认为这不是最好的视觉外观)。这种行为在苹果公司的官方文档中也可以看到:这里可能的解决方案之一是在每次将真实标题添加到节索引标题数组之后添加空字符串,并在
tableView:sectionForSectionIndexTitle:atIndex:
方法中返回索引除以2的结果
这个技巧稍微增加了连续标题之间的距离,但并不能完全解决这个问题。下面是答案的实现,您可以通过在数组上添加更多的空iten来添加更多空间: 首先,让我们用伪索引创建一个数组
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:@""];
}
结果如下:
有一种更简单的方法来完成安东的建议,也可以扩展到每个部分的任意数量的索引(而不是安东建议的两个索引)。因此,您可以添加所需的额外索引标题的数量。在这个例子中,我使用了4 因此,首先在
viewDidLoad
或其他任何地方将这些内容添加到indexTitles
数组中。
注意:如果要向tableView添加内容,也可以动态执行此操作
异步地,这就是我添加if语句的原因,只要它不是第一个
它将用句点替换空字符串。这样就不会有流浪汉了
期末
@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:@""];
}
现在我们可以根据每个索引标题有多少字符串来使用模运算符
- (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的节索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.indexTitles;
}
下面是我制作的一个动态加载内容的:
这是我玩过的东西,很管用。它遵循Anton M的建议,使用Swift 2和ios 9 假设您有以下节索引:
let indexes: [String] = ["A", "B", "C", "D", "E"]
所以每个人都在同一页上,A在索引0处。B是指数1,依此类推
假设在索引之间需要两个空格。然后将节索引定义为:
let indexes: [String] = ["A", "", "", "B", "", "", "C", "", "", "D", "", "", "E"]
现在,A仍然在指数0,但B现在在指数3,C在6,D在9,E在12
您只需计算sectionForSectionIndexTitle中的偏移量。你所追求的是一个真正的标题索引,因为这是你在你的表。因此:
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return index / 3
}
因此,对于索引之间所需的每一个额外空间,需要向上述表达式的枚举数添加1。你可以在操场上轻松地进行测试。我认为你做不到,除非你自己编写控件。截面索引由UITableViewIndex类的对象实现,并且似乎没有任何属性和方法影响线条的间距(请参见)。我会在(并将其发布到Cool。请参阅以供参考。您是如何解决的?有人提出了合适的解决方案吗?@Senior所以还没有找到解决方案?您是否使用此功能定位标签?我使用此功能通过两次重新加载来增加字体大小并正确定位标签……一次在cellforrowatindexpath中,一次在da中ta Arrivest这是一个愚蠢的黑客行为,但如果你能预测你想显示的节标题数量,这是一个简单的修复方法,效果很好。我不知道苹果为什么选择这样改变这个控件!我只是通过@Anton Malmygin解决方案的实现来创建一个答案。
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return index / 3
}