Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the number of cells UITableview displays?

Im using a tableview to display some information in a quiz app that Im working on. My question is how do i make the tableview only show the number of cells that I need. Ive set the number of rows delegate method like this:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return 5;
}

but at the bottom of the table view are empty cells that are not needed. If I set the tableview style to grouped I get 5 cells and no empty ones below them. Ive seen that other people have done this but cant seem to work it out. I was wondering if they have somehow added a custom view to the table footer to cancel the empty cells out?

Any ideas or help appreciated.

like image 341
Martin Harvey Avatar asked May 24 '11 09:05

Martin Harvey


People also ask

How do you add a space between UITableView cells?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.

How do I delete a cell in UITableView?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.


2 Answers

This worked for me - I had extra empty rows at the bottom of the screen on an iphone 5 -

In my case I needed 9 rows

- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    return self.tableView.frame.size.height / 9;
}
like image 170
gheese Avatar answered Oct 12 '22 12:10

gheese


If you do want to keep the separator, you could insert a dummy footer view. This will limit the tableview to only show the amount of cells you returned in tableView:numberOfRowsInSection:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

In swift:

self.tableView.tableFooterView = UIView(frame: CGRectZero)
like image 25
Tom Nys Avatar answered Oct 12 '22 11:10

Tom Nys