12

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?

typeoneerror
  • 55,990
  • 32
  • 132
  • 223
  • 1
    +1 This is good question that is well presented. I wish everyone, myself included, took this much time to compose a good question. – TechZen Apr 10 '10 at 18:10
  • 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](http://stackoverflow.com/questions/14520185/ios-uitableview-displaying-empty-cells-at-the-end) that uses a custom footer view. – Ernesto MB Dec 28 '13 at 03:37

2 Answers2

9

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;
}
TechZen
  • 64,370
  • 15
  • 118
  • 145
Garrett
  • 7,830
  • 2
  • 41
  • 42
  • "it just has the separator style to create that illusion" You're totally right. So, I think I'm just going to add the separator back and make the background image two columns of color instead of having the separator *in* the background image... – typeoneerror Apr 10 '10 at 18:16
  • Nice, that works pretty well! Still a bit of tweaking to do. http://img517.imageshack.us/img517/5091/picture3lt.png – typeoneerror Apr 10 '10 at 18:17
  • 1
    shouldn't `if([source count] <= 7 && indexPath.row > [source count]) {` be `if([source count] <= 7 && indexPath.row > ([source count] - 1)) {` as the index path starts from 0 whereas the count starts from 1 – Alex Jun 09 '11 at 14:41
  • That's not a really future-proof approach. Especially these days where we need to develop for at least 4 different screen heights. – Julian F. Weinert Aug 04 '15 at 08:39
0

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.

TechZen
  • 64,370
  • 15
  • 118
  • 145