0

I am making in iphone app in which there is table showing data it works fine but what i want that if there are only two rows then table does not show other empty space as shown in the screen attached.

enter image description here

you can see the empty space in tableView i want to remove this

    [tableView.layer setCornerRadius:7.0f];
    [tableView.layer setMasksToBounds:YES];
    tableView.layer.borderWidth = 0.5;
    tableView.layer.borderColor = [UIColor grayColor].CGColor;

here is the code for table using border for table.

Queen Solutions
  • 253
  • 2
  • 5
  • 17

5 Answers5

0

put UIview in footer of table view

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
       {
         return 1;
       }
  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; 
      {
           UIView *view=[[[UIView alloc]init]initWithFrame:yourtablefrmae];
         return view; 
      }
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
0

Add this Methods in your code:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
0

The table is just a kind of UIView whose frame is unrelated to the number of rows in it's datasource. You can relate the two, but would need to do so explicitly in code, like this:

// assuming one section and a fixed row height
CGFloat tableHeight = [self.tableView numberOfRowsInSection:0] * self.tableView.rowHeight;
CGRect frame = self.tableView.frame;
self.tableView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, tableHeight);
danh
  • 62,181
  • 10
  • 95
  • 136
0

//In case of custom cells headers footers and any other craps implement this

CGFloat tableHeight = [self.tableView numberOfRowsInSection:0] * self.tableView.rowHeight;
CGRect frame = self.tableView.frame;
self.tableView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width,[self getHeightForTableView:self.tableView]);

//Also add this method to your viewController

-(CGFloat)getHeightForTableView:(UITableView*)tableView
{
    CGFloat tableViewHeight=0.0;

    NSArray *indexPaths=[tableView indexPathsForVisibleRows];
    NSInteger oldsection=((NSIndexPath*)[indexPaths firstObject]).section;

    tableViewHeight+=[tableView.delegate tableView:tableView heightForFooterInSection:oldsection];
    tableViewHeight+=[tableView.delegate tableView:tableView heightForHeaderInSection:oldsection];

    for(NSIndexPath *path in indexPaths)
    {
        if(oldsection!=path.section)
        {
            tableViewHeight+=[tableView.delegate tableView:tableView heightForHeaderInSection:path.section];
            tableViewHeight+=[tableView.delegate tableView:tableView heightForFooterInSection:path.section];
            oldsection=path.section;
        }
        tableViewHeight+=[tableView.delegate tableView:tableView heightForRowAtIndexPath:path];

    }
    return tableViewHeight;
}
Raon
  • 1,266
  • 3
  • 12
  • 25
0

You can remove extra space by using this delegate method

  • (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { // This will create a "invisible" footer return 0.01f; }

For reference go through this link

Eliminate extra separators below UITableView

Community
  • 1
  • 1