How do I change the color of the side Alphabet in an indexed UITableView?

后端 未结 12 810
温柔的废话
温柔的废话 2020-11-27 14:03

I have a table view with an alphabetical index and am using the side alphabet to get through the list quickly. For those not familiar, that uses this:

- (NS         


        
相关标签:
12条回答
  • 2020-11-27 14:30

    Swift 5:

    tableView.sectionIndexColor = .red
    tableView.sectionIndexBackgroundColor = .clear
    
    0 讨论(0)
  • 2020-11-27 14:32

    From what I can tell unfortunately it is not possible to customize the color of the text displayed in the index, the closest I've been able to come is being able to modify the background color and the font of the index.

    There is some code in the iPhone Developers cookbook by Erica Sadun which shows how to access the UITableViewIndex view (an undocumented class). You can find the reference to it on page 175 of the book if you have it. Which gives access to the background color and the font. You can see an unofficial document related to this class here.

    WARNING This is undocumented use of an undocumented class so you need to be cautious about using it.

    Here is a code snippet from the cookbook with minor modifications:

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

    This illustrates how you can access and the UITableViewIndex view and modify it in some aspects. It looks like the view doesn't have any subviews so it is likely doing some custom drawing with the array of index titles.

    It's not perfect but hopefully it helps a little.

    Paul

    0 讨论(0)
  • 2020-11-27 14:32

    Here's the best solution I've found to adjust the background colour of the index bar on the side. It works in iOS7 and probably iOS6.

    Add this to your viewDidLoad

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

    The first one is the default colour, the second is the background colour when you are scrolling.

    0 讨论(0)
  • 2020-11-27 14:33

    There is a way to change the color of the index Alphabet.

    In your header file, declare your UITableView as a property:

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

    Then in your implementation file's viewDidAppear, use the following line:

    //Change the color of the UITableView index color
    _mainTable.sectionIndexColor = [UIColor blackColor];
    
    0 讨论(0)
  • 2020-11-27 14:35

    For iOS7 or Higher:

    self.tableView.sectionIndexColor = [UIColor whiteColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-11-27 14:39

    You can set the tint color for the tableView.

    [[UITableView appearance] setTintColor:[UIColor purpleColor]];
    
    0 讨论(0)
提交回复
热议问题