2009-04-15 53 views
45

我有一个带有英文字母索引的表格视图,并且使用侧面字母表快速浏览列表。对于那些不熟悉的人来说,使用这个:如何更改索引UITableView中边字母的颜色?

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 

我的问题是,我的应用程序刚被剥黑。所以现在很难看到侧面的字母。

我想不出如何改变字母表的颜色。如果可能的话,我希望它是“白色”。

回答

32

从我可以告诉的是,不可能自定义索引中显示的文本的颜色,最近我已经能够修改背景颜色和索引的字体。

Erica Sadun在iPhone Developers食谱中有一些代码,它展示了如何访问UITableViewIndex视图(一个未公开的类)。如果有,请参阅本书第175页的参考资料。它可以访问背景颜色和字体。你可以看到一个与此类here有关的非官方文件。

警告这是无证使用无证类,所以你需要谨慎使用它。

这里是稍作修改食谱的代码片段:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {  

    for(UIView *view in [tv subviews]) 
    { 
    if([[[view class] description] isEqualToString:@"UITableViewIndex"]) 
    { 

     [view setBackgroundColor:[UIColor whiteColor]]; 
     [view setFont:[UIFont systemFontOfSize:14]]; 
    } 
    } 

    //rest of cellForRow handling... 

} 

这说明你如何访问和UITableViewIndex查看和修改它在某些方面。它看起来像该视图没有任何子视图,因此它可能使用索引标题数组进行一些自定义绘图。

这不是完美的,但希望它有一点帮助。

保罗

+3

上述迭代可以在该方法中实现: - (void)viewDidAppear:(BOOL)animated; 这将避免每次滚动tableView时重复迭代。 – 2011-08-02 19:58:36

+1

这种方法只是改变了索引的背景颜色。如果我只想改变字体的颜色而不改变其背景,该怎么办? – 2011-10-20 06:40:03

+1

@DDDhingani改变字体的颜色(在这个例子中为红色),使用:[view performSelector:@selector(setIndexColor :) withObject:[UIColor redColor]]; – rahzark 2013-09-26 14:42:12

1

制作一个可变数组以包含备用标题标签 在

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 

返回的阵列@”“,其中的引号之间的空间的数量决定了HI-的宽度点燃滚动。 让“sectionIndexTitlesForTableView”调用更新标签函数。 在该函数中,删除先前从其超级视图中创建的数组中的所有标签然后创建并添加许多标签。然后将它们添加到表格的超级视图中。

这些是将标签放置在正确位置所需的线。

rect.origin.x = table.frame.origin.x+table.frame.size.width-rect.size.width-2; 
rect.origin.y = 5+table.frame.origin.y+counter *(280-rect.size.height-([customIndexLabels count]-1))/([customIndexLabels count]-1); 
if ([customIndexLabels lastObject]==thisLabel) 
{ 
    rect.origin.y-=10; 
} 

希望有帮助。这并不完美我只是不在乎自己修复它 主要问题是最后一个标签的间距不均匀。

11

我遇到了同样的问题,只是发现了一个这样做的方法,虽然这是使用未记录的类和方法,所以在尝试将其上传到App Store之前多想一段时间。 我应该说,我只用iOS5试过,所以我不知道它是否适用于以前的版本。

我借用并修改了埃里卡·桑德斯食谱例如,以便它现在改变文字颜色为红色:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {  

    for(UIView *view in [tv subviews]) 
    { 
    if([[[view class] description] isEqualToString:@"UITableViewIndex"]) 
    { 
     [view performSelector:@selector(setIndexColor:) withObject:[UIColor redColor]]; 
    } 
    } 

    //rest of cellForRow handling... 

} 
11

我们已经成功地使用下面的代码:

for(UIView *view in [tableView subviews]) { 
    if([view respondsToSelector:@selector(setIndexColor:)]) { 
     [view performSelector:@selector(setIndexColor:) withObject:[UIColor whiteColor]]; 
    } 
} 

工作正常 - 它与Mooglas的答案非常相似 - 但是避免使用单词“UITableViewIndex”。

59

如果您的最低iOS版本比6.0更新,您可以使用sectionIndexColorUITableView的财产。

用于表格视图索引文本的颜色。

@属性(非原子,保留)的UIColor * sectionIndexColor

讨论:

表视图可以显示沿着视线的侧面上的索引,使得它 用户更方便地浏览表的内容很快。此 属性指定用于显示在此区域中的文本的颜色。


更新日期2014年1月13日

我找到一个开源的第三方库:GDIIndexBar帮助自定义索引的外观。

8

您可以设置tableView的色调颜色。

[[UITableView appearance] setTintColor:[UIColor purpleColor]]; 
3

这是我找到的最好的解决方案,用于调整侧面的索引栏的背景颜色。它适用于iOS7和iOS6。

添加到您的viewDidLoad

if ([_tableView respondsToSelector:@selector(setSectionIndexColor:)]) { 
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor]; 
    _tableView.sectionIndexTrackingBackgroundColor = [UIColor whiteColor]; 
} 

第一个是默认的颜色,二是当你正在滚动的背景色。

21

这可以很容易地在UITableView的界面生成器中进行更改 - 无需使用未记录的类?

请看下面的截图,你可以看到字体颜色和背景颜色也可以改变。约伯是个好人!

4

有更改索引字母的颜色的方法。

在你的头文件,声明你的UITableView作为一个属性:

@property (weak, nonatomic) IBOutlet UITableView *mainTable; 

然后在您的实现文件的viewDidAppear,使用以下行:

//Change the color of the UITableView index color 
_mainTable.sectionIndexColor = [UIColor blackColor]; 
16

对于iOS7或更高版本:

self.tableView.sectionIndexColor = [UIColor whiteColor]; 
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor]; 
-1

无证字体更改的快速版:

extension NSObject { 

    class func objectClassName() -> String { 

     let classLongName = reflect(self.self).summary; 

     let tokens = classLongName.componentsSeparatedByString(".") 

     if let typeName = tokens.last { 

      return typeName 
     } 

     return NSStringFromClass(self.dynamicType) 
    } 
} 

func changeSectionIndexFont(tableView: UITableView) -> Void { 

     let realSubviews = tableView.subviews as! [UIView] 

     for subview in realSubviews { 

      if subview.dynamicType.objectClassName() == "UITableViewIndex" { 

       subview.setValue(UIFont(name: "OpenSans", size: 10), forKey: "font") 
      } 
     } 
    }