tarheel
tarheel

Reputation: 4797

UITableView showing more rows than specified in numberOfRowsInSection:

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?

Upvotes: 32

Views: 14604

Answers (9)

CPD
CPD

Reputation: 427

It's a lot easier to:

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

This keeps it simple!

Upvotes: -1

King-Wizard
King-Wizard

Reputation: 15694

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)
}

Upvotes: 24

iosLearner
iosLearner

Reputation: 1331

Short and simple answer..

self.tableView.tableFooterView = [UIView new];

Upvotes: 5

KarenAnne
KarenAnne

Reputation: 2814

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

Upvotes: -1

Wolverine
Wolverine

Reputation: 4329

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.

Upvotes: 8

rdelmar
rdelmar

Reputation: 104082

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.

Upvotes: 0

CrimsonDiego
CrimsonDiego

Reputation: 3616

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.

Upvotes: 59

NSGod
NSGod

Reputation: 22948

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

Upvotes: 0

SimplyKiwi
SimplyKiwi

Reputation: 12444

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.

Upvotes: 0

Related Questions