Semi-Transparent UITableViewCells are not transparent until they have been scrolled off screen
[FIXED] After trying quite a few different solutions I finally got one to work. All I needed to do was set the cell backgroundColor to clear in the willDisplayCell method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
[cell setBackgroundColor:[UIColor clearColor]];
My UITableview has a background image (Photo) and the cells in the table view have a semi-transparent background.
When i first show the UITableView the cells are not showing as transparent. But as soon as I scroll a cell off screen and scroll it back on the cell displays with the semi-transparent background.
Does anyone have any clues as to why it doesn't display correctly until the cell is scrolled off screen? See attached images. First one shows the tableview as soon as its loaded. and the second image shows what it looks like after scrolling the top few cells off screen and back on again.
Below is the code I'm using to setup the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellPhotoIdentifier = @"PhotoDescriptionCell";
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault r开发者_C百科euseIdentifier:CellPhotoIdentifier] autorelease];
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:@"Photo description %i", indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.opaque = NO;
cell.contentView.backgroundColor = [UIColor blackColor];
cell.contentView.opaque = NO;
cell.contentView.alpha = 0.7;
cell.textLabel.backgroundColor = [UIColor clearColor];
return cell;
}
I'm using XCode 4 with IOS SDK 4.3
After re-reading the answer that @progrmr gave the link for, I tried again and managed to get it to work.
I needed to set the Cell Background Color to clear in the willDisplayCell method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
[cell setBackgroundColor:[UIColor clearColor]];
Chances are that you are loading the cells first, and then setting them to be transparent once they are already loaded. Then once you scroll them off screen and back on, they are transparent because you have forced them to be reloaded with the correct settings.
You can fix this buy changing the cell properties in Interface Builder to have the cells be transparent from the start, or after you configure the cell, you need to add code that forces the cell to reload itself. Let me try to find that, as I don't know off the top of my head.
This question should help you figure out cell reloading: Reloading only one UITableViewCell on a UITableview
精彩评论