UITableViewCells appear behind backgroundView
Posted
by Khanzor
on Stack Overflow
See other posts from Stack Overflow
or by Khanzor
Published on 2010-04-19T00:38:00Z
Indexed on
2010/04/19
1:53 UTC
Read the original article
Hit count: 536
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;
}
© Stack Overflow or respective owner