Hide empty cells in UITableView

18,421

Solution 1

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.

Solution 2

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'.

Solution 3

- (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] ;
}
Share:
18,421
minimalpop
Author by

minimalpop

Updated on June 13, 2022

Comments

  • minimalpop
    minimalpop almost 2 years

    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.

  • TomSwift
    TomSwift about 12 years
    @codejunkie - I haven't tried; should work fine but you dont need the autorelease.
  • codejunkie
    codejunkie about 12 years
    @TomSwift same code complains when doing that i guess need to look up more what to replace in iOS5 for autorelease
  • TomSwift
    TomSwift about 12 years
    @codejunkie - if you're using ARC you dont autorelease. Just use [UIView new]
  • codejunkie
    codejunkie about 12 years
    @TomSwift ok i see it hidden cells which were created by default by view "but" it doesnt hide my main problem as i have some empty cell which are selectable but have no-content how can i hide them? i have asked a new question for this.
  • codejunkie
    codejunkie about 12 years
  • Daniel
    Daniel about 12 years
    Although this answer is good. NEVER call new --- always alloc init
  • TomSwift
    TomSwift about 12 years
    @Daniel - I prefer 'new' for parameterless initialization. There's no reason not to use it IMO.
  • Jacob Relkin
    Jacob Relkin almost 12 years
    @Daniel +new is just a convenience method - there's nothing wrong with using it.
  • skwashua
    skwashua about 11 years
    Works great! Tested iOS 6 & ARC
  • mrmuggles
    mrmuggles over 10 years
    self.tableView.tableFooterView = [[UIView alloc] init]; worked but I got an error with autorelease (autorelease is unavailable: not available in automatic reference counting mode)
  • yunas
    yunas over 10 years
    I have updated the answer, hope its more understandable.