views:

587

answers:

4

I'm trying to set the size of my background to be a little shorter than the default, creating some space between the cells. This has proven to be difficult. Setting the frame of the background view seems to do nothing:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSString *reuseIdentifier = @"cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (!cell)
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier] autorelease];

    // Set up the cell...

    cell.contentView.backgroundColor = [UIColor clearColor];

    cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 4, 320, 42)] autorelease];
    cell.backgroundView.backgroundColor = [UIColor blackColor];
    cell.backgroundView.alpha = .2;

    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 4, 320, 42)] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView.alpha = .2;

    cell.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:22.0f];
    cell.selectedTextColor = [UIColor blackColor];
    cell.textColor = [UIColor whiteColor];

    NSDictionary *dict = [files objectAtIndex:indexPath.row];

    cell.text = [dict objectForKey:@"name"];

    return cell;
}

Any help?

Also, setting the selected background view doesn't do anything. When a cell is selected, the background is completely blank. Why is this?

I'm using iPhone OS 2.2.1.

I also do this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.rowHeight = 50.0f;
}

You can download the code here (made a small project for this issue only):

http://dl.dropbox.com/u/608462/tabletest2.zip

+1  A: 

Try this:

UIView *bg = [[UIView alloc] initWithFrame: CGRectInset(cell.frame, 0.0, 2.0)];
bg.backgroundColor = [UIColor whiteColor];
cell.backgroundView = bg;

Also don't forget to set background color and separator color to clear in viewDidLoad():

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
}
sha
The background still covers the entire cell. Is there a working example for this? I'm beginning to think something is wrong with my code, but I can't see what.
quano
I don't have working sample at work. Check parent view controller of your UITableViewController (NavigationController in my case). It has view.backgroundColor property as well. This color should be visible between cells if you follow instructions in my original post.
sha
A: 

Try using:

cell.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 4.0, 320.0, 40.0)]];

For the second question, did you implement:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Rob Jones
Are you saying I have to change the color of the cell in the didSelectRowAtIndexPath method? Then why is there a selectedBackgroundView at all?
quano
No, I'm not suggesting that. Sorry for the confusion. I think it should handle the switch to selectedBackgroundView automatically, unless you did something odd in didSelectRowAtIndexPath.
Rob Jones
Also, this did not make a difference. The background still covers the entire cell.
quano
I think you must be missing something important, although I don't know what. I plugged in this code, as well as setting the backgroundColor into a project of mine and it popped right out. Including the selectedBackgroundView. I'm not sure the bug is in the code you posted.
Rob Jones
Please, look again (posted entire relevant code). Also note I'm using iPhone OS 2.2.1.
quano
+1  A: 

Here's a completely different method from what you're trying.

One thing I like to do is use a custom image for the backgroundView and selectedBackgroundView, rather than let the iPhone handle the coloring tasks. This gives me a lot more flexibility on how the cell is rendered. All it takes is adding something like this:

  cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"normal.png"]];
  cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"selected.png"]];

To:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Rob Jones
This is a nice method, but I only need a single color, so I won't resort to using images just yet. Besides, it's bugging the hell out of me that it doesn't seem to work using UIViews with colors for some reason. I want to get to the bottom of this before proceeding.
quano
Understood. Just thought I'd share.
Rob Jones
Seems like it's not worth bothering trying to make things with UIViews and such, if they're just gonna be a pain. This is the most simple and customizable solution and works the best.
quano
A: 

What i think is happening, is that when you select a row, internally the selectedbackgroundview's alpha value is se to 1, thus showing it completely white.

Ghar
No, the background behind the table is an image, and the bg of the cell is not white but completely transparent (the image has colors so it's easy to see).
quano