63

I have couple of problems with my UITableView.

  1. When I add a UITableview on my page, by default it brings up some fixed number of rows, even though I set number of rows in section as 1. All the rows appear except the first row, and all are empty rows. So, I want to hide all the empty rows in the UItableview.

  2. Based on the non-empty rows, I want to change the height of my UItableView.

Justin
  • 6,611
  • 3
  • 36
  • 57
sbmandav
  • 1,211
  • 3
  • 16
  • 25

11 Answers11

82

NEW ANSWER

In Swift 2.2, 3.0 and onwards, do the following:

tableView.tableFooterView = UIView()

OLD ANSWER BELOW. KEPT FOR POSTERITY.

If you must use UITableViewStylePlain, and you don't use a footerView for anything else, you can use the following semi-dirty solution if you have ARC enabled.:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];

    return view;
}

If you have ARC disabled, use the following:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[[UIView alloc] init] autorelease];

    return view;
}

This creates an invisible footerView, that appears immediately after the last data-filled cell.

ArtSabintsev
  • 5,170
  • 10
  • 41
  • 71
  • 1
    I hope you don't mind, but you were leaking memory by allocating a new view and passing that off, so I inserted an autorelease in there. If this was ARC-enabled code, I apologize, but someone complained about it leaking in practice, so I wanted to secure against that. – Brad Larson Nov 06 '12 at 22:30
  • Sure, no problem. I tend to write my answers in ARC-enabled fashion. I'll make it known in my future posts that I'm writing in such a manner. Thanks for the heads up on the edit. – ArtSabintsev Nov 07 '12 at 16:36
  • 1
    Note that the last time I checked, 93% of all iOS devices were running 6.0 or higher so there really is no reason to write non-ARC-enabled code anymore. – memmons Aug 27 '13 at 14:23
  • 1
    I agree with you, but the case was different 10 months ago, which is why Brad decided to change my code and add the autorelease syntax. – ArtSabintsev Aug 27 '13 at 14:50
  • 1
    Yep, understood. It was more a hint that maybe it's time to change it back :D. – memmons Aug 27 '13 at 15:32
  • Awesome. Here is the swift code related to it: func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let view = UIView() return view } – Julio Bailon Apr 29 '16 at 14:15
  • 2
    **SWIFT 3.0** works great with `tableView.tableFooterView = UIView()` – Marcelo Gracietti Dec 22 '16 at 23:50
54

You sure you are using UITableViewStyleGrouped style?

Because by default it is set to UITableViewStylePlain which shows empty cells too after displaying filled cells.

pablasso
  • 2,479
  • 2
  • 26
  • 32
Sanniv
  • 1,892
  • 1
  • 17
  • 21
  • Just this one simple change lets me avoid the always hacky feel of using a single space String as a section header title as well as removing the empty cells. How am I just finding out about this now?? – Zig Aug 04 '17 at 18:22
  • Unfortunately this causes Xcode 9 to crash, see https://stackoverflow.com/questions/46390437/xcode9-0-in-storyboard-when-i-toggle-tableview-style-to-grouped-xcode-crashed – PJ_Finnegan Nov 10 '17 at 12:39
34
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
 { 
     return 0.01f;
 }

and for iOS 7

 self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Sachin
  • 47
  • 3
  • 10
17

I solved the problem by creating a simple UIView as footer view with the same background color as the table background:

(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

Maybe you have to disable scrolling in your table view in addition.

edorian
  • 38,542
  • 15
  • 125
  • 143
Katlu
  • 496
  • 6
  • 14
11

If your tableview has multiple sections, you may try using this:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == tableView.numberOfSections - 1) {
        return [UIView new];
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == tableView.numberOfSections - 1) {
        return 1;
    }
    return 0;
}
codyko
  • 654
  • 6
  • 10
11
- (void) viewDidLoad
{
  [super viewDidLoad];
  self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
}
yunas
  • 4,143
  • 1
  • 32
  • 38
6

This can also be done through Interface Builder. Simply add a 1 pixel tall UIView as a footer to the UITableView. It's essentially the same as most of the answers here, but it keeps some UI specifics in the view instead of in the view controller.

enter image description here

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
6
[self.SomeTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Use this to get rid of the lines if the UITableView is empty.

Code Guru
  • 393
  • 5
  • 15
0

A bit hidden in Bourne's answer; if you want to hide to bottom empty rows in a plain tableview with multiple sections, use this answer:

https://stackoverflow.com/a/5658100/580173

Community
  • 1
  • 1
ejazz
  • 2,458
  • 1
  • 19
  • 29
0

in the method that returns the number of rows, specify the count yourself dynamically. For example if you are going to populate the table with say a NSArray named arrayForRows:

return [arrayForRows count]; // inside the method which returns the number of rows.

The above is a simple example populating a table with an array as a datasource. There are no empty rows. Only that many rows show up in the table according to the count of items in the array populating the table.

I think you mean height of the row in the table. You can play around with the height of the rows by using the method:

(CGFloat)tableView:(UITableView  *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath

read http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

EDIT: ah now I get what you are trying to achieve. You are trying to avoid showing the empty rows separated with those separator lines right? See this post:

how to display a table with zero rows in UITableView

Community
  • 1
  • 1
Bourne
  • 10,094
  • 5
  • 24
  • 51
  • well, i would like you to check the same example on your end.My array returns only count 1. and there is only one row which has the data and under that there are 9 empty rows.I want to hide those empty rows. and also i did not mean the row height. the table height. that is okay, i can take of that. But my 1st question still remains unanswered. – sbmandav Nov 18 '10 at 09:47
  • check the EDIT above. Is that what you are looking at? I used the same solution at my end to remove the lines. If I had 5 rows with data, the 5 would show up but the rest of the table in the screen had line separators. The only way out is a grouped table style instead of plain (unfortunately that defeated the purpose of my data view). – Bourne Nov 18 '10 at 09:58
  • HI..i am not talking about edit.... when the viewdidload happens, you know that table will be loaded. By default all the empty rows will be created. if you specify the no.of rows in section, that many times cellforedit delegate willl be exeecuted and creates the cell content.But you can see there are other empty rows created. i want to remove those rows at some particular event. – sbmandav Nov 18 '10 at 12:47
  • so in a way you want the plain table style to look like a grouped one? if there are 5 items in your datasource array for instance, then the table must show only 5 line separated rows? In that case a workaround might be to make it a grouped style tableview with just one section and number of rows as indicated by the count of your datasource – Bourne Nov 18 '10 at 12:55
0

For Swift:

override func viewWillAppear(animated: Bool) {
    self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

// OR 

    self.tableView.tableFooterView = UIView()
}

For C#: (Please don't forget to add using CoreGraphics; )

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);
    this.sampleTableview.TableFooterView = new UIView(frame: CGRect.Empty);
}
Alvin George
  • 14,148
  • 92
  • 64