2

I'm using a UITableView control on a Nib with one section and set the delegate/data source to my controller where the "table data" array is initialized to 2 items only. However, I'm seeing additional empty rows being rendered in the Table View when run in the simulator. How can I make the UITableView render only two rows?

Thanks!

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
java_pill
  • 843
  • 3
  • 13
  • 23
  • Can you please paste your `- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView` and `- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section` code. Do you use grouped table views? – sn3ek Jun 04 '12 at 23:05
  • 1
    can you show your code please? How are you adding data to your cells? – Hosni Jun 04 '12 at 23:12

7 Answers7

2

I was having a similar problem, how to show only separators for the cells that contain data. The tableView renders as many rows as it can fit into the area allocated to it (width and height). The number you designate in numberOfRowsInSection is only used to determine how many times it should call cellForRowAtIndexPath.

To overcome the the rendering of separator lines for empty cells I did the following:

  1. Disable separators for the whole tableView. You can do that in the inspector for the tableview in Interface builder or by calling [yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];.

  2. Inside your cellForRowAtIndexPath where you populate your tableview with cells create a new UIView and set it as a subview to the cell. Have the background of this view lightgray and slightly transparent. You can do that with the following:

    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
                                    (0, cell.frame.size.height-1, 
                                       cell.frame.size.width, 1)];
    [separatorView setBackgroundColor:[UIColor lightGrayColor]];
    [separatorView setAlpha:0.8f];
    [cell addSubView:separatorView];
    

    The width of this view is 1 pixel which is the same as the default separator, it runs the length of the cell, at the bottom.

Since cellForRowAtIndexPath is only called as often as you have specified in numberOfRowsInSection these subviews will only be created for the cells that possess data and should have a separator.

Hope this helps.

Hrafn
  • 2,867
  • 3
  • 25
  • 44
0

You need to make sure that you are using the arrays count as the number of cells in the section.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myArray count];
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • 1
    Yes I'm using that value however, in the simulator there are more rows rendered. You can try it out w a simple sample. – java_pill Jun 04 '12 at 23:10
  • 1
    If you are using your arrays count, are you sure there are only 2 objects? Not possible to have more cells than what you define in `numberOfRowsInSection:` – WrightsCS Jun 04 '12 at 23:11
  • 1
    If (whoever) you are going to down vote an answer, at least leave a comment as to why. – WrightsCS Jun 05 '12 at 00:51
0

set

urtableview.frame=(x,y,width,[rowheight * num of rows])
0

If you do not need sections or footers, give the table an empty footer.

In viewDidLoad add

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

See How to remove empty cells in UITableView?

Another way to accomplish this is to return an empty footer view. This is especially useful if you need a footer for a previous section.

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

Finally, here is another link with other ways to accomplish this.

Eliminate extra separators below UITableView

Community
  • 1
  • 1
sho
  • 741
  • 1
  • 7
  • 16
0

Add an empty uiview as bottom view for the tableview

tabelview.tableFooterView = UIView()

-1

You could just decrease the size of the table view's frame to be 2*cellHeight.

Or whatever the height necessary. So say each cell is 44 pixels in height and you only have one section, so no section headers, you would do:

self.tableView = [[UITableView alloc] 
  initWithFrame:CGRectMake(x-coord, y-coord, width, 88) 
  style:UITableViewStylePlain];

That should only show 2 cells, assuming they are 44 pixels in height each.

anon_dev1234
  • 2,143
  • 1
  • 17
  • 33
  • Alternatively, you could set your table to be grouped, then disable the background through: self.tableView.backgroundColor = [UIColor clearColor], and if on iPad self.tableView.backgroundView = nil – anon_dev1234 Jun 05 '12 at 00:14
-1

You can call this method from your viewDidLoad:

- (void) addFooter
{
 UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
 v.backgroundColor = [UIColor clearColor];
 [self.tableView setTableFooterView:v];
}
tarheel
  • 4,727
  • 9
  • 39
  • 52
  • What does adding a footer view have to do with the number of rows being produced?? – WrightsCS Jun 05 '12 at 00:20
  • 1
    java_pill wasn't asking how to produce only 2 rows, java_pill was asking how to get rid of the fact that a UITableView by default fills the containing UIView with lines regardless of content. This fixes that problem. – tarheel Jun 05 '12 at 01:12
  • I think you need to read the question again. **However, I'm seeing additional empty rows being rendered in the Table View when run in the simulator** – WrightsCS Jun 05 '12 at 01:37
  • 1
    Yes, *additional rows*, aka rows beyond what the methods numberOfSectionsInTableView: and numberOfRowsInSection: tell the UITableView to render. – tarheel Jun 05 '12 at 02:00
  • This is what **you** just said "*was asking how to get rid of the fact that a UITableView by default fills the containing UIView with lines regardless of content.*" which contradicts your last comment. – WrightsCS Jun 05 '12 at 02:05