3

I have a UITableView with cells having data from server response which means not everytime all the cells displays the data. So I need to remove those "empty" cells.I have already searched and could not find any possible solution. The solution from How to remove empty cells in UITableView? didn't work out. Actually it made my tableview disappear.

I need another solution other than the addition of a footer view. Is there any?

Community
  • 1
  • 1
hamedazhar
  • 990
  • 10
  • 26

7 Answers7

8

You should check your data which is coming from the server and only add not nil data to tableview data source.

After that reload your tableview.

This way your tableview won't have any empty cells. simple.

In case your problem is that number of cell being displayed doesn't fill up your view

you can try

tableview.tableFooterView = UIView()
rv7284
  • 1,092
  • 9
  • 25
  • I've already checked and if there is non-null data only the tableview gets reloaded. otherwise an alert will show with "no data" message. – hamedazhar Feb 01 '17 at 06:41
  • I'm talking about the particular data for each cell, just run the server response through "for" loop and add only not nil data into data source – rv7284 Feb 01 '17 at 06:44
  • @Hamed i just update my answer take a look – rv7284 Feb 01 '17 at 08:22
4

You can use one of the two methods.

1. self.tableView.tableFooterView.frame = CGRectZero;
2. self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
vaibhav
  • 4,038
  • 1
  • 21
  • 51
Dinu
  • 49
  • 3
1

try this

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
   return [[UIView alloc]init];
}
Ravi
  • 2,441
  • 1
  • 11
  • 30
1

If you are talking about separators; You can remove that by changing property "separator" of UITableView.

enter image description here

But this will also affect data cells. You must add custom separator line in your data cell.

Amit Tandel
  • 883
  • 7
  • 16
  • op's ques is about to remove empty cell not for cell separator, have you seen? – vaibhav Feb 01 '17 at 06:45
  • Actually, my issue was to remove empty cells, I've found the solution. Just give table view height constraint in the storyboard and drag the same into your .h file. Now add the following code just before/after you reload the table: **tableviewHeight=[array count]*[row height];**. But this resulted in a cell separator for the final cell which I don't need. So I combined your solution with mine and its working perfectly now! – hamedazhar Feb 01 '17 at 07:05
0

I hope you are adding data in cell by array. So only add data in array when you receive the data from server. If you do not receive data from server don't add anything. And return numberOfRows your arrays count.

Sunny
  • 821
  • 6
  • 17
0

To remove empty cells at bottom of the tableview

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

or use this

self.tableView.tableFooterView = UIView()
ThamaraiD
  • 21
  • 2
0

You can do it from nib also.

From source:

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 0.01f;
    }

     - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
     {        
       return [UIView new];
    }
Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49