【发布时间】:2011-07-12 09:50:48
【问题描述】:
我想显示一个索引表视图。
默认情况下,部分索引标题为灰色。
如何改变它们的颜色?
【问题讨论】:
-
我想你没有得到我的问题。我不想更改节标题视图,而是显示在右侧的节索引标题的颜色
标签: ios iphone uitableview
我想显示一个索引表视图。
默认情况下,部分索引标题为灰色。
如何改变它们的颜色?
【问题讨论】:
标签: ios iphone uitableview
编辑/跟进 iOS7(2013 年 9 月)阅读 Graham Perks 的回答以跟进 iOS7,他解释了 sectionIndexTrackingBackgroundColor 和 sectionIndexBackgroundColor
为未来的读者跟进(2012 年 9 月)这个问题。
从 iOS 6 开始,现在可以更改索引颜色文本和背景。为此提供了两个函数(请参阅 iOS 6 文档)。
sectionIndexColor
表格视图的索引文本使用的颜色。
@property(nonatomic, retain) UIColor *sectionIndexColor
表格视图可以在视图的侧面显示一个索引,使用户更容易快速浏览表格的内容。此属性指定用于在该区域中显示的文本的颜色。
可用性:适用于 iOS 6.0 及更高版本。
声明于:UITableView.h
sectionIndexTrackingBackgroundColor
表格视图的索引背景区域使用的颜色。
@property(nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor
表格视图可以在视图的侧面显示一个索引,使用户更容易快速浏览表格的内容。此属性指定当用户拖动手指时在索引背景中显示的颜色。
可用性:适用于 iOS 6.0 及更高版本。
声明于:UITableView.h
【讨论】:
sectionIndexTrackingBackgroundColor 是用手指跟踪时的背景颜色。在 iOS7 中,要更改正常的背景颜色,请使用 sectionIndexBackgroundColor。
在您的 viewDidLoad 中添加以下代码(用于 swift):
tableView.sectionIndexColor = UIColor.lightGrayColor()
【讨论】:
在 iOS 7 中,要更改 正常背景颜色,请使用 sectionIndexBackgroundColor 属性。
sectionIndexTrackingBackgroundColor 是用手指跟踪时的背景颜色。
(感谢 HpTerm 提供在 iOS 6 上运行良好的初始指针。)
【讨论】:
更改索引部分背景颜色和文本颜色的优化方式
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor whiteColor];//[UIColor colorWithRed:77.0/255.0 green:162.0/255.0 blue:217.0/255.0 alpha:1.0];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableSection"]]];
}
【讨论】:
我想我刚才读错了问题。
如果您的意思是让您导航 tableview 的部分标题的 A-Z 索引,那么它不能更改,暂时不可能。或者您可以创建自己的。
跟进
这只适用于iOS6之前。 iOS6及以上请参考HpTerm的回答。
【讨论】:
可能的重复 here 表示“未记录”的做法。我在生产代码中使用它,它可以工作(未针对 5.0 下的 iOS 测试)。
【讨论】:
抱歉,您不能更改索引的颜色。
【讨论】:
抱歉,有点晚了,但是对于所有正在寻找答案的人来说,这里是 Swift 代码
for view in tableView.subviews {
if view.respondsToSelector("setIndexColor:") {
view.performSelector("setIndexColor:", withObject: UIColor.yourColor())
}
}
然后,如果您想获取部分索引作为视图并使用它做您想做的事情,这里是:
for view in tableView.subviews {
NSStringFromClass(view.classForCoder) == "UITableViewIndex" {
// do what you want
// e.g: view.hidden = true
}
}
【讨论】:
只需将表格视图的色调更改为您想要的颜色。
【讨论】: