32

I want my tableView to show 6 rows with text in it, in this case "Example." As far as I can tell, I have my numberOfSectionsInTableView: and numberOfRowsInSection: set properly. See example code below:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   // Return the number of sections.
   return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  // Return the number of rows in the section.
  return 6;
}

- (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];
  }

  cell.textLabel.text = @"Example";

  return cell;
}

The problem is when you see the image below showing lines for rows that shouldn't/don't exist.

enter image description here

How do I get rid of the lines showing past row 6?

tarheel
  • 4,727
  • 9
  • 39
  • 52
  • 3
    This is the default behavior of all plain table views with fewer rows than is needed to scroll, so simply setting the number of rows and sections won't help. You'll need to find another way... – BoltClock May 27 '12 at 04:04

9 Answers9

59

The generally accepted way of doing this is to add a footer view with a frame size of CGRectZero, as such:

[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]

What this does is tell the table that there is a footer, and so it stops displaying separator lines. However, since the footer has a CGRectZero as its frame, nothing gets displayed, and so the visual effect is that the separators simply stop.

Community
  • 1
  • 1
CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
  • How can I get rid of the extra cells? I don't want to show them - I'd rather show the gray background that shows in section headers. – Pratik Stephen Aug 06 '15 at 03:20
24

Swift Version

The easiest method is to set the tableFooterView property:

override func viewDidLoad() {
    super.viewDidLoad()
    // This will remove extra separators from tableview
    self.tableView.tableFooterView = UIView(frame: CGRect.zero)
}
Prashant Ghimire
  • 4,890
  • 3
  • 35
  • 46
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
8

This is Because of Your Table-view Height. Weather you have Write

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

// Return the number of rows in the section. return 6; }

But its show rows According to Table-view Size. If you Dont want to show This extra Lines then Make UITableView Style Plain To Grouped.

Wolverine
  • 4,264
  • 1
  • 27
  • 49
  • -1 for UITableViewStyleGrouped. This affects the entire display of the table, not what the questioner asked for. Removing the separators is a side effect of changing the style. If the grouped style is changed in later versions to include additional separators then this style change is moot. – Tim Mar 05 '14 at 04:25
  • Nice one. Nice, easy, quick fix! – Mike Critchley Oct 07 '17 at 22:10
5

Short and simple answer..

self.tableView.tableFooterView = [UIView new];
iosLearner
  • 1,312
  • 1
  • 16
  • 30
0

You could do something along the lines of:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
[self.mytableView cellForRowAtIndexPath:indexPath].hidden = YES;

Im sure there are some better ways but this is the first thing that came to mind.

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • Forgive me if this is a stupid question, but where are you suggesting I put this code? (inside what method?) – tarheel May 27 '12 at 04:54
0

If you're referring to the light gray lines that appear below the last row, that's simply the default way a UITableView draws the row separator.

You could try changing the Separator style in Interface Builder (see the images below) to see if one of those might be more to your liking.

enter image description here enter image description here

NSGod
  • 22,699
  • 3
  • 58
  • 66
  • Thanks for the suggestion, but I want the separator there for the rows that do exist, but none of the rest showing. – tarheel May 27 '12 at 04:55
0

You didn't say what you do want to see past the last row. If you just want to see the window background, then just embed your table view in a UIView that's just tall enough to show the number of rows you want to see. If you want to see more rows without scrolling, then you would have to adjust the size of that containing view based on the number of rows.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
-1

To programmatically remove it, use this: [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

KarenAnne
  • 2,764
  • 1
  • 28
  • 21
-1

It's a lot easier to:

  1. return numberOfSections + 1
  2. return 0 rows in the final section

This keeps it simple!

CPD
  • 427
  • 2
  • 11