Search code examples
iosobjective-cxcode4backgroundmaster-detail

Master-Detail Application Background


I'm having a problem with my master-detail app background. When I start the app instead of showing the background image that I set up it shows this: enter image description here

And when I press the add button it change to the real background: enter image description here

This is the code that I'm using:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }

    return self;
}

- (void)viewDidLoad
{

        [super viewDidLoad];

    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];


    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.backgroundView = bgView;
    static NSString *CellIdentifier = @"Cell";
    tableView.separatorColor = [UIColor colorWithHue:0 saturation:0 brightness:33 alpha:0.2];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    }

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cell.png"]];
    cell.textLabel.backgroundColor = [UIColor clearColor];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = object;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

How can I make it so it shows directly the background image? Thanks.


Solution

  • Instead of making another view, just put this in viewDidLoad

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];
    self.tableView.backgroundView = nil;
    

    and remove these

    //in viewDidLoad
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper.png"]];
    //in cellForRowAtIndexPath:
    tableView.backgroundView = bgView;
    

    OR try putting

    tableView.backgroundView = bgView;
    

    in viewDidLoad and it should work.

    The reason you were having this problem was because you set the backgroundView of the tableView in a method that gets called when cells are set up, but because there were no cells in the table view yet, that method doesn't get called until there is at least one cell in the view.