12

I have a searchable tableview, but would like to only display the number of cells that are returned once a user starts a search. For instance, if a user types in a charecter and there is only one cell that matches the search, the remaining cells the remaining cells of the table are empty. I was wondering if there is a way to hide those empty cells. I was also wondering if there is a way to hide the table view if a person's search does not match any data (by default, it displays a table of empty cells saying "No Results").

Thanks.

minimalpop
  • 6,997
  • 13
  • 68
  • 80

7 Answers7

61

Set the tableFooterView to some dummy view ([[UIView new] autorelease] will work fine). This prompts the UITableView to show the footer (which will have zero size) in place of the empty 'cells'.

TomSwift
  • 39,369
  • 12
  • 121
  • 149
23
- (void) viewDidLoad
{
  [super viewDidLoad];
 *// self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];*   
  *//Since memory management is controlled by ARC now, so ignore autorelease*
  self.tableView.tableFooterView = [[UIView alloc] init] ;
}
yunas
  • 4,143
  • 1
  • 32
  • 38
  • self.tableView.tableFooterView = [[UIView alloc] init]; worked but I got an error with autorelease (autorelease is unavailable: not available in automatic reference counting mode) – mrmuggles Dec 22 '13 at 23:04
  • I have updated the answer, hope its more understandable. – yunas Dec 23 '13 at 23:47
5

Change your implementation of tableView:numberOfRowsInSection: in your data source to return the number of search results instead of the total number of items. Then populate those cells with the search results instead of with the rest of the data.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

Simple you can use this..

tableView.tableFooterView = [UIView new];
iosLearner
  • 1,312
  • 1
  • 16
  • 30
0

For swift 2.x xCode 7 :

override func viewDidLoad() {
        super.viewDidLoad()
        print("∙ \(NSStringFromClass(self.dynamicType))")
        ...
        self.tableView.tableFooterView = UIView()
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0
- (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.

0

Hiding an empty cells in Swift

Swift 3 Edition

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.tableView.tableFooterView = UIView(frame: CGRect.zero)
}

Swift 2 Edition

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
Mannopson
  • 2,634
  • 1
  • 16
  • 32