I want to setup 2 completely different look(and content) of a cell. (one selected, and one not selected).
This code works, BUT, because the image for the selected cell was 50% transparent, I can see the BackgroundView on bottom.
In clear, selectedBackgroundView offer a nice way to populate a cell with element only visible when selected. Is there another location than BackgroundView to populate cell, a location how will not appear on selectedBackgroundView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"hello";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
UIImage *image = [UIImage imageNamed:@"listview_btn.png"];
UIImage *imageThumb = [UIImage imageNamed:@"01_thumb.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:imageThumb];
[imageView setFrame:CGRectMake(0, 0, 50, 67)];
[cell setBackgroundView:[[UIImageView alloc]initWithImage:image] ];
[cell.backgroundView addSubview:imageView];
UIImage *imageSel = [UIImage imageNamed:@"listview_btn_on.png"];
UIImage *imageThumbSel = [UIImage imageNamed:@"01_thumb_Sel.jpg"];
UIImageView* imageViewSel = [[UIImageView alloc] initWithImage:imageThumbSel];
[imageViewSel setFrame:CGRectMake(110, 0, 50, 67)];
[cell setSelectedBackgroundView:[[UIImageView alloc]initWithImage:imageSel] ];
[cell.selectedBackgroundView addSubview:imageViewSel];
Make the selected background view opaque. Since it is always inserted above the background view, you can composite them in Photoshop or another graphics editor and use the result as the selected background view.
If you cannot afford this approach (I still highly recommend it), you can subclass UITableViewCell
and override both setHighlighted:animated
and setSelected:animated
to set the background view property to nil when selected/highlighted and restore it when deselected/unhighlighted. However it is a much more involved approach, IMHO.
{}
button. It will add proper markdown indention so your code is readable and gets the nice color scheme :- - Matthias Bauch 2012-04-04 02:55