6

Default focus color of UITableViewCell is white colour. How can we change the focus colour of UITableView cell?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
GAURAV VIG
  • 269
  • 2
  • 9

4 Answers4

3

You can set this as in the code below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    cell.focusStyle = UITableViewCellFocusStyleCustom;
    // ...
    return cell;
}

- (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if ([context.previouslyFocusedView isKindOfClass:[UITableViewCell class]])
        context.previouslyFocusedView.backgroundColor = [UIColor clearColor];
    if ([context.nextFocusedView isKindOfClass:[UITableViewCell class]])
        context.nextFocusedView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
}
Leszek Szary
  • 9,763
  • 4
  • 55
  • 62
2

It is a swift code. Please convert to Objective-C and try it. It maybe help you.

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

if let nextFoc = context.nextFocusedView as? YourCellName{

       nextFoc.backgroundColor = UIColor.redColor()

}

if let prevFocus = context.previouslyFocusedView as? YourCellName{

        prevFocus.backgroundColor = UIColor.clearColor()

}

Look it a screen shot.

initial focus imageSecond focus imageThird focus image

Sankalap Yaduraj Singh
  • 1,167
  • 2
  • 14
  • 32
  • Tried same in Objective C... but still the focus colour of cell does not change – GAURAV VIG Dec 09 '15 at 14:19
  • Where did you write this code, on my side it's working properly. You will write this code into the custom cell class. – Sankalap Yaduraj Singh Dec 10 '15 at 05:33
  • Custom cell background changes...but the default focus style animation of tableviewcell(parallax) goes away. – GAURAV VIG Dec 10 '15 at 09:13
  • Look it this link may be help you. http://stackoverflow.com/questions/31373775/swift-how-do-i-create-a-parallax-effect-in-uitableview-with-uiimageview-in-thei – Sankalap Yaduraj Singh Dec 11 '15 at 14:40
  • 2
    Thanks for the help... I found one more solution to it. I added a view on custom cell and mapped it to cell background view. So when tableview focus update method is called i just change the background view colour...then the colour changes with default tableviewcell parallax animation – GAURAV VIG Dec 12 '15 at 03:02
  • @GAURAVVIG Thanks, I had the same problem and your last solution works perfectly. – jabu.10245 Dec 14 '15 at 06:50
1

If you want to maintain the fancy parallax cells you can first set a background view on your custom cell:

self.backgroundView = UIView()

And in your viewcontroller with the tableview do something like this:

  func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    (context.nextFocusedView as! MyCustomTableviewCell).backgroundView!.backgroundColor = UIColor.purpleColor()
    if let prev = context.previouslyFocusedView as? MyCustomTableviewCell {
      // Set the color back to whatever it was, in this case I have a black background with black cells
      prev.backgroundView?.backgroundColor = UIColor.blackColor()
    }
  }
CodyMace
  • 646
  • 1
  • 8
  • 19
1

If you are using custom tableViewCell, then you can use the delegate method of the tableViewCell as below,

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (self.focused)
    {
        [self showHighlightedCellStyle];//set highlighted bg color
    }
    else
    {
        [self showNormalCellStyle];//reset the bg color
    }
}
Ram Gandhi
  • 707
  • 8
  • 24