371

i am trying to display a simple UITableView with some data. I wish to set the static height of the UITableView so that it doesn't displays empty cells at the end of the table. how do I do that?

code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d", [arr count]);
    return [arr count];
}
Michal
  • 15,101
  • 9
  • 71
  • 103
z22
  • 9,823
  • 17
  • 68
  • 124

10 Answers10

923

Set a zero height table footer view (perhaps in your viewDidLoad method), like so:

Swift:

tableView.tableFooterView = UIView()

Objective-C:

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

Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.

Interface builder pro-tip:

If you are using a xib/Storyboard, you can just drag a UIView (with height 0pt) onto the bottom of the UITableView.

Andy
  • 14,260
  • 4
  • 42
  • 56
104

Swift 3 syntax:

tableView.tableFooterView = UIView(frame: .zero)

Swift syntax: < 2.0

tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

Swift 2.0 syntax:

tableView.tableFooterView = UIView(frame: CGRect.zero)
Viktor Kucera
  • 6,029
  • 3
  • 31
  • 43
Dustin Williams
  • 3,902
  • 2
  • 15
  • 19
66

In the Storyboard, select the UITableView, and modify the property Style from Plain to Grouped.

Gabriel Diaconescu
  • 1,699
  • 3
  • 21
  • 30
17

Implemented with swift on Xcode 6.1

self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.tableFooterView?.hidden = true

The second line of code does not cause any effect on presentation, you can use to check if is hidden or not.

Answer taken from this link Fail to hide empty cells in UITableView Swift

Community
  • 1
  • 1
Leonardo Jorge
  • 177
  • 1
  • 3
  • This did it for me using Swift & XCode 6.1! Had tried Swift-equivalent versions of the answers above but with no success. – Chris Nov 24 '14 at 16:47
11

I can not add comment as of now so adding this as an answer.

@Andy's answer is good and the same results can be achieved with the following line of code:

tableView.tableFooterView = [UIView new];

'new' method belongs to NSObject class and invokes alloc and init methods for UIView.

Rishil Patel
  • 1,968
  • 3
  • 13
  • 29
Sahil
  • 479
  • 5
  • 12
  • This works out OK, but isn't technically the same as Andy's answer. Using new simply calls the "init" method, but there's no guarantee that [myView init] and [myView initWithFrame:CGRectZero] are the same, even though in today's implementation, they appear to be. – smehmood Jul 08 '14 at 00:34
8

I tried the code:

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

In the viewDidLoad section and xcode6 showed a warning. I have put a "self." in front of it and now it works fine. so the working code I use is:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Shekhar Gupta
  • 6,148
  • 3
  • 29
  • 50
Filozof Abi
  • 81
  • 1
  • 1
3

or you can call tableView method to set the footer height in 1 point, and it will add an last line, but you can hide it too, by setting footer background color.

code:

func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
     return 1
}

looks like last line

KostiaZzz
  • 174
  • 14
2
override func viewWillAppear(animated: Bool) {
    self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

/// OR 

    self.tableView.tableFooterView = UIView()
}
Michal
  • 15,101
  • 9
  • 71
  • 103
Alvin George
  • 13,650
  • 87
  • 63
1

Using UITableViewController

The solution accepted will change the height of the TableViewCell. To fix that, perform following steps:

  1. Write code snippet given below in ViewDidLoad method.

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

  2. Add following method in the TableViewClass.m file.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        return (cell height set on storyboard); 
    }
    

That's it. You can build and run your project.

Manan Devani
  • 434
  • 3
  • 15
0

in the below method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66)
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65)); 
    }
    else
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66);
    }
    return [array count];
}

here 65 is the height of the cell and 66 is the height of the navigation bar in UIViewController.

P.J.Radadiya
  • 1,402
  • 11
  • 17