10

How can i show UITableView Rows only which contains data not the other rows. By default UITableView shows 10 rows. If we have data for three rows It will display three rows only How can I implement this? Thank you.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
nitesh meshram
  • 281
  • 2
  • 4
  • 10
  • If you want to remove the extra separators below a plain style table view, check out this answer: http://stackoverflow.com/questions/1369831/eliminate-extra-separators-below-uitableview-in-iphone-sdk/1789714#1789714 – albertamg Jun 20 '11 at 11:35

7 Answers7

32

You can set a footer view and make it invisible if you want. Separators will appear only for rows which contain data:

self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];
Dimitris
  • 682
  • 3
  • 14
  • 19
17

Try this ..

self.tableView.tableFooterView = [UIView new];
iosLearner
  • 1,312
  • 1
  • 16
  • 30
2

I don't think it is possible to hide the extra separators using a standard way. This behavior is preset.

Plain styled tableView shows the row/separator even if there are no real rows exist. Only Grouped styled tableView shows only the existing rows.

Edit: As suggested by others, you can add a footer view to tableView to hide those extra separators.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • you can eliminate the extra separators by [adding a footer view to the table](http://stackoverflow.com/questions/1369831/eliminate-extra-separators-below-uitableview-in-iphone-sdk/1789714#1789714). – albertamg Jun 20 '11 at 11:39
0

Add a static cell that fills the rest of the screen.

Steve Ham
  • 3,067
  • 1
  • 29
  • 36
0

Just add below code in your viewDidLoad() method.

self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Darshan
  • 2,272
  • 3
  • 31
  • 43
0

OK, I assume you are using Interface Builder.

The easy way:

  1. make sure you have chosen your view controller class as both UITableViewDelegate and UITabbleViewDataSource.

  2. In the ViewController.h (or whatever its called) add <UITableViewDelegate,UITableViewDataSource> before the { of the @interface definition. to the class that it must conform to those protocols.

  3. Make sure the minimal datasource protocol methods are in the class :

    – tableView:cellForRowAtIndexPath:  
    – tableView:numberOfRowsInSection: 
    
  4. in the – tableView:numberOfRowsInSection: method return the number of desired rows - 3 or by a bit of code to calculate this property

  5. in the – tableView:cellForRowAtIndexPath: method do what you need to on the cell and return it. Boilerplate code

    is:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    
     if (cell == nil) 
     {
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
     }
    
    cell.text = @"I am a cell";
    return cell;
    }
    

N.B. If you are not using Interface Builder do: tableView.delegate = self; tableView.datasource = self;

mdb
  • 52,000
  • 11
  • 64
  • 62
Martin
  • 1,135
  • 1
  • 8
  • 19
-2

You can specify the required number of rows in the return parameter of the following function :

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

    return NUMBER_OF_ROWS;
}

For more details please go through the Apple documentation of UITableView.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html

HG's
  • 818
  • 6
  • 22