UITableViewController background image

Discussion in 'iOS Programming' started by xaphann, Jun 5, 2012.

  1. xaphann macrumors newbie

    Oct 22, 2011
    Have an UITableViewController with a custom cell and want to add an image "behind" it. Let me try to describe it; there is enough room for six cells to be on the screen at a time. If there is less then 6 then below the last cell the image should appear. The image should not scroll with the cells, it is designed to take up the full screen of the iphone (mean 360x470 pixels). No blank cells should appear.

    In the viewDidLoad;
    self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.png"]];
    self.tableView.backgroundColor = [UIColor clearColor];
    Then in the cellForRowAtIndexPath method;
    cell.accessoryView.backgroundColor = [UIColor redColor];
    note: red is not the final color of the cell, the background will be an image. The red is for testing (and the cell image is not done :D ).

    The problem is that with this code the cells appear to be transparent (you can see the background image on cells that contain data), there are blank cells displayed (so for example if only 2 cell have data 4 blank cells are also displayed) and the image is not stationary it scrolls (therefore it loops and the image is not designed to loop).

    FYI the numberOfRowsInSection is set with this;
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    thanks for the help
  2. dantastic macrumors 6502a


    Jan 21, 2011
    First of all, UITableViewController can't do what you want to do. UITableviewcontroller is evil, stay away from it. Createa UIViewController, drop a UITableview onto it and create a property and connect delegates.

    This way you will be able to assign a view with a clear color to the background view and also you can add a uiimageview under your uitableview.
  3. xaphann thread starter macrumors newbie

    Oct 22, 2011
    Really? There is no better answer? That means a lot of re-doing work
  4. xaphann, Jun 6, 2012
    Last edited: Jun 6, 2012

    xaphann thread starter macrumors newbie

    Oct 22, 2011
    Ok, it was not as much work as I thought but I am still getting the same results. So this is what I did, created a new UIViewController, and re-did the code to point to UITableView in the UIViewController. The data and all functionality works. Then add an UIImage behind the UITableView. Running the app at this point will result in nothing showing up. Adding this code makes the image appear;
    self.tableView.backgroundColor = [UIColor clearColor];
    Issue is that just like before the image scrolls with the table and blank cells appear.
  5. xaphann thread starter macrumors newbie

    Oct 22, 2011
    Fixed it!

    For those that care here are the basic steps. Add the UIImage to the back of the view controller. Then add this code to your viewDidLoad;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    Then add the following method;
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    	//Set a color
            cell.backgroundColor = [UIColor whiteColor];
    	//Set an image
            //cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myCellImage.png"]];
    Thank for the help and pointing me in the right direction!
  6. MattInOz macrumors 68030


    Jan 19, 2006
    Not sure if this is to late to be helpful, I know I hit the same issue a while back.

    In myTableViewController and within the -viewDidLoad method I have the these two lines of code and it works.

    UIImageView *boxBackView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TextureBoxboard.jpg"]];
    [self.tableView setBackgroundView:boxBackView];
    but it's only visible on screen if there are no cells on top of it or the cell background colour is set clear.

Share This Page