z22 z22 - 7 days ago 5
iOS Question

How to remove empty cells in UITableView?

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

Answer

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

Objective-C:

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

Swift 2.0:

tableView.tableFooterView = UIView()

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

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

Comments