【问题标题】:Subclass UITableView to change font colour of the section index?子类 UITableView 更改部分索引的字体颜色?
【发布时间】:2012-05-01 14:56:25
【问题描述】:

我有一个深色背景,上面有一个UITableView。默认情况下,部分索引是半透明的,带有深色文本颜色。我想将部分索引的文本颜色更改为与 UITableViewCell 标题标签相同的颜色。我已经阅读了一些内容,看来您必须将UITableView 子类化?我该怎么做?

【问题讨论】:

标签: iphone cocoa-touch uitableview


【解决方案1】:

从 iOS 6 开始,您可以这样做:

searchTable.sectionIndexColor = [UIColor blackColor];

【讨论】:

    【解决方案2】:

    为了解决这个问题,我在 viewDidAppear 中使用了以下内容:

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

    由于没有记录,它必须通过选择器。

    【讨论】:

    • 赞成使用私有 API 有被 App Store 拒绝的风险。我不知道 Apple 的自动代码审查是如何工作的,但您可以考虑将 @selector(setIndexColor:) 替换为 NSSelectorFromString(@"setIndexColor:")。此外,在条件中使用respondsToSelector: 比检查类描述更安全。
    【解决方案3】:

    从 iOS 6.0 开始,有两种方法可让您更改部分索引的颜色以及拖动滑块时显示的背景。

    if ([tableView respondsToSelector:@selector(setSectionIndexColor:)]) {
        tableView.sectionIndexColor = ... // some color
        tableView.sectionIndexTrackingBackgroundColor = ... // some other color
    }
    

    当然,这只会在设备有 6.0+ 的情况下执行。对于任何较旧的 iOS,什么都不会改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-23
      • 2011-03-18
      • 2011-10-29
      • 1970-01-01
      • 2010-10-19
      • 2011-02-19
      • 1970-01-01
      • 2013-01-03
      相关资源
      最近更新 更多