How can I set View Background color?

Discussion in 'iOS Programming' started by mikezang, Sep 8, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I have a View with two table views inside it. I want to set top of table view's background to cyan color, I used code as below, the cell color is getting to cyan, but the table view didn't, how can I set it to cyan color?
    Code:
        tableView.backgroundColor = UIColor.cyanColor;
    
        cell.labelDate.backgroundColor = [UIColor cyanColor];	
        cell.labelOpen.backgroundColor = [UIColor cyanColor];	
        cell.labelHigh.backgroundColor = [UIColor cyanColor];	
        cell.labelLow.backgroundColor = [UIColor cyanColor];	
        cell.labelClose.backgroundColor = [UIColor cyanColor];	
        cell.labelVolume.backgroundColor = [UIColor cyanColor];	
    
     

    Attached Files:

  2. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    That looks to me like you want to set the background color of the table cell.
     
  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    Top table is only one row, I want to whole row background is set, though I can set cell background color, but there is gap between cells, I hope that gap is also the same background color, may I make sense?
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Ah, my apologies. Didn't pick up on that subtlety.

    As of iOS 3.2, you also need to make sure your backgroundView is nil in order for the background color to come through.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    There's also a problem where the table sets the background color of cells because, well because, it feels like it. To adjust for this you have to set the background color of cells in the willDisplayCell callback. Maybe that's the problem.
     
  7. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #7
    This. I believe the intention is for the background colour to match the background of the content view for consistency, which is why UITableView does this.

    Like you say, you should set this kind of stuff in willDisplayCell as its the last thing to be called before the cell is displayed.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    I think the reason it does this is related to selection of the cells. Apple considers the background color of the cell to be "state" of the cell and reserves the right to change it. This changed in OS 3.2. Prior to that setting the background color could be done once and it would stay set. On the Apple iPhone forum after some complaints about this new behavior replied that Apple had always reserved the right to change the background color of cells.

    I think this is a bad design. No other views get their background colors changed arbitrarily. UIButton is a good comparison. It does get its appearance changed in response to touches. It has properties that describe the background color depending on the state of the button. UITableViewCell should have something similar. Nevertheless, it is what it is.
     

Share This Page