12
votes

I am trying to customize a UITableView. So far, it looks good. But when I use a custom UITableViewCell sub-class, I do not get the blank table cells when there's only 3 cells:

alt text http://img193.imageshack.us/img193/2450/picture1zh.png

Using the default TableView style I can get the repeating blank rows to fill the view (for example, the mail application has this). I tried to set a backgroundColor pattern on the UITableView to the same tile background:

UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"score-cell-bg.png"]];
moneyTableView.backgroundColor = color;

...but the tile starts a bit before the TableView's top, so the tile is off once the actual cell's are done displaying:

alt text http://img707.imageshack.us/img707/8445/picture2jyo.png

How can I customize my tableview but still keep the blank rows if there's less rows than fill a page?

2
+1 This is good question that is well presented. I wish everyone, myself included, took this much time to compose a good question. - TechZen
I was trying to formulate the same question and found two different approaches. As a reference for those who came here from google, check this other answer that uses a custom footer view. - Ernesto MB

2 Answers

9
votes

Did you by chance remove the background color and separator style? If you did, that could be why there are no extra cells. I would think the default UITableView doesn't add more cells really, it just has the separator style to create that illusion and because it has a white background, they look like cells.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

If that's not the case, you could always try adding extra cells that can't be selected:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ([source count] <= 7) ? 7 : [source count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set all labels to be blank
    if([source count] <= 7 && indexPath.row > [source count]) {
        cell.textLabel.text = @"";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        cell.textLabel.text = [source objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }

  return cell;
}
0
votes

I believe the slight misalignment of the top row is caused by the vertical scroll bounce of the tableview. If you turn that off, the top row should align properly.

Also, you can just return a cell height that will encompass the tile in tableview:cellHeightForRow:. That works on the default cells nicely.