views:

215

answers:

1

I am having a problem setting the background of UITableView to a UIImageView (see below for why I am doing this), once the view is set, it works fine, and scrolls with the UITableView, but it hides the elements of the Table View.

I need to have a UIImageView as the background for a UITableView. I know this has been answered before, but the answers are to use:

[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];

Or something like (which I need to use):

UIImageView *background = [MainWindow generateBackgroundWithFrame:tableView.bounds];
[tableView addSubview:background];
[tableView sendSubviewToBack:background];

The reason I need to use the latter is because of my generateBackgroundWithFrame method, which takes a large image, and draws a border around that image to the dimensions specified, and clips the remainder of the image:

+ (UIImageView *) generateBackgroundWithFrame: (CGRect)frame {
    UIImageView *background = [[[UIImageView alloc] initWithFrame:frame] autorelease];
    background.image = [UIImage imageNamed:@"globalBackground.png"];
    [background.layer setMasksToBounds:YES];
    [background.layer setCornerRadius:10.0];
    [background.layer setBorderColor:[[UIColor grayColor] CGColor]];
    [background.layer setBorderWidth:3.0];

    return background;
}

Please note: I understand that this might poorly effect performance, but I don't have the resources to go through and make those images for each potential screen in the app. Please do not answer this question with a mindless "you shouldn't do this" response. I am aware that it is possibly the wrong thing to do.

How do I show my UITableView control elements? Is there something that I am doing wrong in my delegate? Here is a simplified version:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = 
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
                initWithFrame:CGRectMake(20, 20, 261, 45)
                reuseIdentifier:CellIdentifier] autorelease];

        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        UIImage *rowBackground;         
        NSString *imageName = @"standAloneTVButton.png";    
        rowBackground = [UIImage imageNamed:imageName];     

        UITextView *textView = 
                [[UITextView alloc] initWithFrame:CGRectMake(0, 5, 300, 200)];

        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor blackColor];
        textView.font = [UIFont fontWithName:@"Helvetica" size:18.0f];

        Purchase *purchase = 
                [[PurchaseModel productsPurchased] objectAtIndex:indexPath.row];

        textView.text = [purchase Title];

        selectedTextView.text = textView.text;


        UIImageView *normalBackground = 
                [[[UIImageView alloc] init] autorelease];

        normalBackground.image = rowBackground;

        [normalBackground insertSubview:textView atIndex:0];

        cell.backgroundView = normalBackground;
        [textView release];
    }

    return cell;
}
A: 

So the answer to this problem so far is to add a tag to the background view, and then do the following:

- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)path {
    [tableView sendSubviewToBack:[tableView viewWithTag:BACKGROUNDVIEW_TAG]];
}

I don't really like this, as I'm doing this for every cell, but I can't seem to find a allCellsDidLoadForTableView: (UITableView *)tableView sort of method.

Khanzor