UITableView background image view won't appear until after I tap cell

Discussion in 'iOS Programming' started by moonman239, Nov 8, 2014.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    Hi,

    I seem to be having a really strange problem. As you can see in the code below, I have an image view that is supposed to show up in the background of a UITableView when the view controller's view is loaded. For some reason, whether the background image view creation code is placed in viewDidLoad or viewDidAppear, the background image won't show until after I tap one of the table view's cells.

    Here's the code:

    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *roadImagePath = [[NSBundle mainBundle] pathForResource:@"Word Family App Screen Blank Road" ofType:@"png"];
        CGRect tableViewFrame = [self.tableView frame];
        CGRect roadViewFrame = (CGRect){CGPointZero,tableViewFrame.size};
        roadViewFrame.size.height = 300;
        UIImage *image = [UIImage imageWithContentsOfFile:roadImagePath];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [imageView setFrame:roadViewFrame];
        [imageView setUserInteractionEnabled:false];
        [self.tableView setBackgroundView:imageView];
    
    Please help, and thank you!
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    Please help me!

    I have two ideas, neither of which are particularly desirable:

    1) Use a NIB file & a subclass of UITableView
    2) Create a UIView, put the table view & an image view in that view, and send the image view to the back.
     
  3. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #3
    Would you mind posting the contents of your didSelectRowAtIndexPath: method?
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    Not quite sure if it'll help, but here it is:
    Code:
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *segueIdentifier;
        NSInteger row = [indexPath row];
        NSInteger section = [indexPath section];
        if (section < [[self elements] count] - 1)
        {
        switch (row) {
            case 0:
                segueIdentifier = @"xxxxSegue";
                break;
            case 1:
                segueIdentifier = @"xxVCSegue";
                break;
            default:
                break;
        }
        }
        else
        {
            segueIdentifier = @"xxxSegue";
        }
        [self performSegueWithIdentifier:segueIdentifier sender:[NSNumber numberWithInteger:[indexPath section]]];
    }
    
     
  5. TheWatchfulOne macrumors 6502

    TheWatchfulOne

    Joined:
    Jun 19, 2009
    #5
    It might help if that's when your tableView background image starts displaying correctly. I thought you might be doing something in that method that could be done in viewDidLoad or viewDidAppear instead, but from what you've posted, nothing like that stands out.

    I see you are using storyboards, which I have not used myself. The question I would be asking myself is this:

    Is there anything about a storyboard that would cause you not to be able to manipulate the tableView programmatically?

    Code:
    [self.tableView setBackgroundView:imageView];
    I'm not aware of any reason why you shouldn't be able to, but like I said I haven't used storyboards.
     
  6. moonman239, Nov 13, 2014
    Last edited: Nov 13, 2014

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #6
    To answer your question, I am not aware of anything that would prevent me from accomplishing my goal. Also, I tried that line of code, but that didn't work. Also, the cells are not opaque (at least, they're not supposed to be).

    Edit: I think the problem was with the image itself. You see, the image was mostly white. But I just did some cropping. Then I could kind of see what I wanted to see.
     

Share This Page