Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can't seem to change UITableView background color.

pendraggon87pendraggon87 Posts: 31Registered Users
edited May 2012 in iOS SDK Development
I am using a grouped table view, and want to change the background to either an image or a color, but no matter what i do, including tableView.backgroundColor or [tableView setBackgroundColor: nothing seems to work!

Thanks
Post edited by pendraggon87 on
«1

Replies

  • zig_zag_lovezig_zag_love Posts: 45Registered Users
    edited August 2008
    try setting the tableView's background property in the viewDidLoad method of your view controller - that's what I do, and it works for both grouped and ungrouped tables
  • pendraggon87pendraggon87 Posts: 31Registered Users
    edited August 2008
    tried that - did nothing. What is going is:

    UIViewController is managing a view i made in interface builder. This view has a uitableview in it, which uses the UIViewController as a delegate and data source. Does that make a difference?
  • zig_zag_lovezig_zag_love Posts: 45Registered Users
    edited August 2008
    Okay, have you implemented any of the methods of the delegate/datasource yet? I wrestled with the problem of how to change the background color of a grouped table for about a week before I got it right - also, does the background color change when you set the table to be in an indexed style, as opposed to a grouped style?
  • pendraggon87pendraggon87 Posts: 31Registered Users
    edited August 2008
    The delegate and data source are set as the view controller - seems to work, as I set it to display two blank cells and it works fine. But nothing seems to happen with the color. The reason I have this UITableView in the regular view file anyway is that I need it to be centered on the screen, and not take up the whole width (like, only display five lines) and I never got the hang of setting positions and everything for the frame.
  • pendraggon87pendraggon87 Posts: 31Registered Users
    edited August 2008
    Somehow I think that even though the delegate and data source variables work, the variable doesnt work. I have my header as:

    IBOutlet UITableView *tableView;

    @property (nonatomic, retain) UITableView *tableView;

    So this should work...
  • pendraggon87pendraggon87 Posts: 31Registered Users
    edited August 2008
    Ok, finally got the color working - just changed the name of the variable to tView instead of tableView...not sure why that did it. Is there a way to set an image in the background? So I can make a pseudo frame for the table?
  • adeemadeem Posts: 34Tutorial Authors
    edited August 2008
    what i did for putting image as background for tableview.

    To add a background image open your .xib file make sure you have a in
    that .xib file if you don't add a "View" and then make your table
    apart of the "View" file.

    1. On the view file move the "Table View" file down a little.
    2. Then add your image the the "View" file.
    3. Select the image file and on the menu click Layout>Send To Back.
    4. Realign the "Table View"
    5. With the "Table View" selected go to inspecter and click the
    Background Color. At the bottom just above your custom colors
    there is a slider bar for opacity. Change this to the desired
    opacity.
    6. Save the file and "Build and Go"

    You should have an image in the background when that view loads.



    and at the end, write this code back to controller
    
    tableView.backgroundColor = [UIColor clearColor];
    
    
    Adeem
    www.GenITeam.com
    Adeem<br />
    For Tutorials on iPhone check out <a href="http://adeem.me/blog"; target="_blank">http://adeem.me/blog</a><br />
    iPhone Tutorial on Demand!
  • StitchStitch Posts: 401Registered Users @ @
    edited August 2008
    Very nice adeem,

    Are you able to change the background image inside your code rather than setting it inside IB?
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=311941449&mt=8"; target="_blank">BUZZER!</a> : <font color="Sienna">iTunes Library Music Quiz (1 or 2 Player)</font>
  • ashash Posts: 54Registered Users
    edited December 2008
    Well do it programatically you will need to have a UITableView object in your class, so when loadView method of your view controller is called, do something like this
    
    [super loadView];
    	UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    	[contentView addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YOUR BACKGROUND IMAGE"]]];
    	
    	UITableView *tempTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]style:UITableViewStyleGrouped];
    	tempTable.delegate =self;
    	tempTable.dataSource =self;
    	tempTable.backgroundColor = [UIColor clearColor];
    	self.myTableView = tempTable;
    	[contentView addSubview:tempTable];
    	[tempTable release];
    	
    	self.view = contentView;
    	[contentView release];
    

    Unfortunately, i wasn't able to do this if my class extended from UITableViewController

    Hope this helps.
    --<br />
    Ashar<br />
    <a href="http://www.tkxel.com"; target="_blank">www.tkxel.com</a>
  • dboykodboyko Posts: 16Registered Users
    edited January 2009
    ash wrote: »
    Well do it programatically you will need to have a UITableView object in your class, so when loadView method of your view controller is called, do something like this
    
    [super loadView];
    	UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    	[contentView addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YOUR BACKGROUND IMAGE"]]];
    	
    	UITableView *tempTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]style:UITableViewStyleGrouped];
    	tempTable.delegate =self;
    	tempTable.dataSource =self;
    	tempTable.backgroundColor = [UIColor clearColor];
    	self.myTableView = tempTable;
    	[contentView addSubview:tempTable];
    	[tempTable release];
    	
    	self.view = contentView;
    	[contentView release];
    

    Unfortunately, i wasn't able to do this if my class extended from UITableViewController

    Hope this helps.

    But I have to do if I have UITableViewController???
    Please help!!!!
  • ashash Posts: 54Registered Users
    edited January 2009
    Well i just thought of a very dirty hack (assuming you're extending from UITableViewController) ... in the loadView method try something like this
    [super loadView];
    [self.tableView removeFromSuperview];
    
    // add the Background Image to self.view here
    
    [self.view addSubview:self.tableView];
    

    i didn't get a chance to try this myself
    --<br />
    Ashar<br />
    <a href="http://www.tkxel.com"; target="_blank">www.tkxel.com</a>
  • eholsingereholsinger Posts: 2New Users
    edited June 2009
    the simplest way to do this is to put this in your numberOfSectionsInTableView override method, once, at the top of the method and - bang - done - no weird hacks:

    for a single-section table:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    [tableView setBackgroundColor:[UIColor blackColor]];
    return 1;
    }

    cheers, eric.


    ash wrote: »
    Well i just thought of a very dirty hack (assuming you're extending from UITableViewController) ... in the loadView method try something like this
    [super loadView];
    [self.tableView removeFromSuperview];
    
    // add the Background Image to self.view here
    
    [self.view addSubview:self.tableView];
    

    i didn't get a chance to try this myself
  • isaaclimdcisaaclimdc Posts: 17Registered Users
    edited August 2009
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	tableView.backgroundColor = [UIImage imageNamed:@"hawaii.jpg"];
    	return 1;
    }
    

    Is there a way to use this method to set an image as background? I know the above code doesn't make sense, but is there such a way? This is because I coded my tableView without IB and so cannot use IB to set an imageView.
  • Shahid RasheedShahid Rasheed Posts: 19Registered Users
    edited August 2009
    I also coded my tableView without IB and following code is working for me. I have placed this code in viewWillAppear Method.
    -
    UIImage *backgroundImage = [UIImage imageNamed:@"test.png"];
    UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
    self.tableView.backgroundColor = backgroundColor;
    [backgroundColor release];
    



    isaaclimdc wrote: »
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	tableView.backgroundColor = [UIImage imageNamed:@"hawaii.jpg"];
    	return 1;
    }
    

    Is there a way to use this method to set an image as background? I know the above code doesn't make sense, but is there such a way? This is because I coded my tableView without IB and so cannot use IB to set an imageView.
  • isaaclimdcisaaclimdc Posts: 17Registered Users
    edited August 2009
    I also coded my tableView without IB and following code is working for me. I have placed this code in viewWillAppear Method.
    -
    UIImage *backgroundImage = [UIImage imageNamed:@"test.png"];
    UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage:backgroundImage];
    self.tableView.backgroundColor = backgroundColor;
    [backgroundColor release];
    

    Thanks! That worked... however, how do you programatically set the table view style to grouped instead of plain?
  • Shahid RasheedShahid Rasheed Posts: 19Registered Users
    edited August 2009
    isaaclimdc wrote: »
    Thanks! That worked... however, how do you programatically set the table view style to grouped instead of plain?

    I use initWithStyle to create my tableView

    SettingsMenuController *settingMenuController = [[SettingsMenuController alloc] initWithStyle:UITableViewStyleGrouped];
  • isaaclimdcisaaclimdc Posts: 17Registered Users
    edited August 2009
    I use initWithStyle to create my tableView

    SettingsMenuController *settingMenuController = [[SettingsMenuController alloc] initWithStyle:UITableViewStyleGrouped];

    Where do I put this if I'm trying to load my RootViewController? In the AppDelegate?
  • Shahid RasheedShahid Rasheed Posts: 19Registered Users
    edited August 2009
    isaaclimdc wrote: »
    Where do I put this if I'm trying to load my RootViewController? In the AppDelegate?

    RootViewController contains the array of view Controller. You have to push your table view controllers into that array.

    You can write this code before adding your table view controller into that array . It totally depends where you inserting elements into that array of rootViewController. Normally people fill that array in viewDidLoad method of rootViewController. There must be one place where you create the instance of your tableViewController class before inserting into root view controller array and pushing the controller into stack. you only needs to create that instance with initWithStyle.
  • skayserskayser Posts: 36Registered Users
    edited August 2009
    eholsinger wrote: »
    the simplest way to do this is to put this in your numberOfSectionsInTableView override method, once, at the top of the method and - bang - done - no weird hacks:

    for a single-section table:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	[tableView setBackgroundColor:[UIColor blackColor]];
        return 1;
    }
    

    cheers, eric.

    This worked perfectly for me rather than some of the complex methods I've seen on the web. Thanks!!
  • NavneNavne Posts: 1New Users
    edited August 2009
    Hi,

    Just want to add my 2 cents worth of a good feature based off information from this post.

    Setting the background color to clearcolor as follows...
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    [tableView setBackgroundColor:[UIColor clearColor]];
    return 1;
    }

    ... will enable you to set the backgroundcolor for each cell. Otherwise the text labels view (the table view background) is opaque and will prevent the cell background to show through.

    In this way you can create the "famous" white/light blue tableview (or white/grey as in my example ;)
    The tag may not be needed in this piece of code (extracted) but setTag is very useful...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@TableViewCell];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc]
    initWithFrame:CGRectZero
    reuseIdentifier:@TableViewCell] autorelease];
    }

    int row = [indexPath row];
    [[[cell subviews] objectAtIndex:0] setTag:111];
    ...
    ...
    //set your cell text
    cell.text = @Some Text; //OS 2.0 style....

    UIView *cellView = [cell viewWithTag:111];
    if (row % 2){
    [cellView setBackgroundColor:[UIColor whiteColor]];
    }
    else {
    [cellView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0 ]];
    }
    return cell;
    }

    Have fun...
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,689Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited August 2009
    Edit: nevermind. Misread the issue.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • bahmedbahmed Posts: 1New Users
    edited March 2010
    Hi all,

    I am developing an iPad application in which I need a table view ( style grouped ) having background color as clearColor.

    The problem is that the following method does have any effect and table background stays grey

    [self.tableView setBackgroundColor:[UIColor clearColor]];


    Is this a bug in iPad-sdk-3.2 or I am doing something wrong.

    Thanks,

    Bill
  • HackHack San FranciscoPosts: 2,137Registered Users @ @ @ @
    edited April 2010
    I have the 3.2 SDK but am targeting 3.1. WHen i create a tableview in Interface builder i set the style to Grouped but When i launch my app it is still in the list style. IS there something i need to change in interface builder to support 3.1?

    Thanks
  • mAlleXXXmAlleXXX Posts: 1New Users
    edited April 2010
    bahmed wrote: »
    The problem is that the following method does have any effect and table background stays grey
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    Bill

    The problem is that UITableView contains background view, containing gray gradient image (or blue lines in iPhone version),
    so You need to disable it, or set another view to change the background.

    e.g.
    [tableView setBackgroundView:nil];
  • ltgbaultgbau Posts: 18Registered Users
    edited October 2010
    mAlleXXX wrote: »
    The problem is that UITableView contains background view, containing gray gradient image (or blue lines in iPhone version),
    so You need to disable it, or set another view to change the background.

    e.g.
    [tableView setBackgroundView:nil];

    Great, works like a charm for me.
    Thanks!!!
    App in AppStore:<br />
    <a href="http://appsfire.com/f2"; target="_blank">Multi Protractor</a>
«1
Sign In or Register to comment.