Tom
Tom

Reputation: 2418

Hide Null UItableView Cells/Rows

I currently have a uitableview in place.

The data is obtained from an sqlite file.

Each column of the sqlite file will represent a list of: labels, images etc

The code i am using to get rows is

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  AppDelegate* appdelegate = (AppDelegate*) [[UIApplication sharedApplication]delegate];
  return [appdelegate.arrayDatabase count];

}

The code to populate the cells

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *simpleTableIdentifier = @"simpleTableIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if (cell == Nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
  }

  AppDelegate * appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
  ShowsList *sShows = [appdelegate.arrayDatabase objectAtIndex:indexPath.row];
  cell.textLabel.text = [sShows strName ];
}

The problem i am having:

I want to use the labels column to populate the cell text of ViewController1.tableview but it is returning the null rows and displaying empty rows/cells in the view.

However I want to hide the rows by either counting the null cells and applying the count to the numberOfRowsInSection: method or by simply hiding empty cells.

Hide empty cells in UITableView & How to hide a section in UITableView? looks like something similar but didn't resolve my issue.

Could somebody please point me down the correct path or provide the answer :)

Thanks so much

Thomas

Upvotes: 1

Views: 1967

Answers (5)

Nameet
Nameet

Reputation: 650

In your viewDidLoad set the table footer as:

[yourTableView.tableFooterView setFrame:CGRectMake(0, 0, yourTableView.frame.size.width, 1)];

The footer is not used. But making its frame to sizeZero might crash the application, so use footer with height 1. This worked for me. Hope it helps.

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Assuming you are getting @"" in the model from which you are populating the UITableView.

You can remove that blank like this:

[_arrays removeObjectIdenticalTo:@""];

And use this filtered array as your model to populate the tableview.

Upvotes: 0

Mert
Mert

Reputation: 6065

just implement a reloadTableData method like that

- (void)reloadTableData
{
// do a reload maybe from db and get yourobjects eg dbItems
// _tableItems = [NSMutableArray array];
    for (ShowsList *list in dbItems) {
        if ([list strName].length) {
            [_tableItems addObject:list];
        } 
    } 
}

then just use _tableItems array to populate your tableview

use [self reloadTableData] instead [self.tableview reloadData]

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  return _tableItems.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *simpleTableIdentifier = @"simpleTableIdentifier";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  if (cell == Nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
  }

  ShowsList *sShows = [_tableItems objectAtIndex:indexPath.row];
  cell.textLabel.text = [sShows strName ];
}

Upvotes: 1

Apurv
Apurv

Reputation: 17186

You should create a mutable copy of your data object. Iterate to it first. Remove null/blank entries. Now use this modified collection for tableViewDelegate methods.

Upvotes: 0

P.J
P.J

Reputation: 6587

Add this code

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

OR

There is alternate solution

you can set height of UITableView in numberOfRowsInSection function, having height as [array count]*(cell height)

Hope it helps you..

Upvotes: 2

Related Questions