【问题标题】:How do I change the color of the side Alphabet in an indexed UITableView?如何更改索引 UITableView 中侧面字母的颜色?
【发布时间】:2010-10-19 12:06:55
【问题描述】:

我有一个带有字母索引的表格视图,并且正在使用侧面字母快速浏览列表。对于那些不熟悉的人,使用这个:

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

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

我不知道如何更改字母的颜色。如果可能的话,我希望它是“白色的”。

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    不幸的是,据我所知,无法自定义索引中显示的文本颜色,我能做到的最接近的是能够修改索引的背景颜色和字体。

    Erica Sadun 编写的 iPhone 开发者食谱中有一些代码显示了如何访问 UITableViewIndex 视图(一个未记录的类)。如果你有的话,你可以在本书的第 175 页找到它的参考资料。这可以访问背景颜色和字体。可以看一个非官方的与这个类相关的文档here

    警告这是对未记录类的未记录使用,因此您需要谨慎使用它。

    这是来自食谱的代码 sn-p,稍作修改:

    - (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 视图并在某些方面对其进行修改。看起来该视图没有任何子视图,因此它可能正在使用索引标题数组进行一些自定义绘图。

    它并不完美,但希望它会有所帮助。

    保罗

    【讨论】:

    • 上面的迭代可以在方法中实现:-(void)viewDidAppear:(BOOL)animated;这将避免每次滚动 tableView 时重复迭代。
    • 这个方法只是为我改变了索引的背景颜色。如果我只想更改字体颜色而不更改背景颜色怎么办?
    • @DipDhiningani 改变字体的颜色(在这个例子中为红色),使用这个:[view performSelector:@selector(setIndexColor:) withObject:[UIColor redColor]];
    • 由于 UIKit 组件视图层次结构在过去已知会发生变化,如果您要使用此方法,那么我建议搜索整个子视图树(而不是第一级)。可以找到这样做的递归方式here
    • [查看 setFont:[UIFont systemFontOfSize:14]]; setFont 不适合我.. 我们如何设置字体大小
    【解决方案2】:

    创建一个可变数组以包含备用标题标签 在

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

    返回一个 @" " 数组,其中引号之间的空格数决定了高亮滚动条的宽度。 让“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;
    }
    

    希望对您有所帮助。它并不完美,我只是不太关心自己修复它 主要问题是最后一个标签的间距不均匀。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,只是找到了一种方法,尽管这是使用未记录的类和方法,所以在尝试将其上传到 App Store 之前要多考虑一下。 我应该说我只在iOS5上尝试过,所以我不知道它是否适用于以前的版本。

      我借用并修改了 Erica Saunders 食谱示例,现在它将文本颜色更改为红色:

      - (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...
      
      }
      

      【讨论】:

        【解决方案4】:

        我们已经成功使用了以下代码:

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

        效果很好 - 它与 Mooglas 的答案非常相似 - 但避免使用“UITableViewIndex”这个词。

        【讨论】:

          【解决方案5】:

          如果您的最低 iOS 版本高于 6.0,您可以使用 UITableViewsectionIndexColor 属性。

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

          @property(nonatomic, retain) UIColor *sectionIndexColor

          讨论:

          表格视图可以在视图的侧面显示一个索引,使其 用户更容易快速浏览表格的内容。这 属性指定用于在该区域中显示的文本的颜色。


          更新日期 2014.1.13

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

          【讨论】:

          • 完美答案!谢谢你。
          • 完美...像魅力一样工作:)
          • 酷。我不知道为什么,但在 iOS 8.1 中,默认的索引背景和文本颜色是蓝色的。感谢您的提示,我可以将“self.tableView.sectionIndexBackgroundColor”更改为白色背景。
          【解决方案6】:

          您可以为 tableView 设置 tint 颜色。

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

          【讨论】:

            【解决方案7】:

            这是我找到的调整侧面索引栏背景颜色的最佳解决方案。它适用于 iOS7 并且可能适用于 iOS6。

            将此添加到您的 viewDidLoad

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

            第一个是默认颜色,第二个是滚动时的背景颜色。

            【讨论】:

              【解决方案8】:

              这可以在 UITableView 的界面构建器中轻松更改 - 无需使用未记录的类?

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

              【讨论】:

                【解决方案9】:

                有一种方法可以改变索引字母的颜色。

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

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

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

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

                【讨论】:

                  【解决方案10】:

                  对于 iOS7 或更高版本:

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

                  【讨论】:

                    【解决方案11】:

                    用于未记录字体更改的 Swift 版本:

                    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")
                                }
                            }
                        }
                    

                    【讨论】:

                      【解决方案12】:

                      斯威夫特 5:

                      tableView.sectionIndexColor = .red
                      tableView.sectionIndexBackgroundColor = .clear
                      

                      【讨论】:

                      • 在 Swift 5 中也可以使用
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-11-26
                      • 1970-01-01
                      相关资源
                      最近更新 更多