0

So, most of the questions are "my tableView only shows the first row, what's wrong?". Well, what I need is exactly the opposite.

I have a SearchBar (not Search Display Controller) and until the user starts typing, I want to show ONLY the first row and nothing more.

My TableView's content is Dynamic Prototypes, with 2 Prototype Cells. The first is the only one I want to show, but it shows others in blank.

https://www.dropbox.com/s/q4ak6z3gbc0gh5c/Screenshot%202014-07-22%2011.24.22.png

This is my tableView:numberOfRowsInSection:.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRows = 0;

    if ([self.searchBar.text isEqualToString:@""]) {
        numberOfRows = 1;
    }

    return numberOfRows;
}

All the help will be very appreciated! \o/

edmoks
  • 27
  • 7

4 Answers4

1

Probably there is a better solution, but as proposed before :

CGRect frame = [self.tableView frame];

frame.size.height = [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

self.tableView.frame = frame;
EDUsta
  • 1,932
  • 2
  • 21
  • 30
0

In your view controller's -tableView:numberOfRowsInSection: make sure tableView equals to self.tableView, and then you return 1:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return 1;
    } else {
        return yourDataModel.count;
    }
}
Sunwoo Park
  • 386
  • 3
  • 15
  • Yes I did, but your screenshot only shows a simulator with only one cell in the table view, which can be achieved exactly in this way. – Sunwoo Park Jul 22 '14 at 14:52
  • @edmoks If you meant hiding the separator between cells, see this question: http://stackoverflow.com/questions/286332/how-do-i-remove-the-borders-of-a-uitableview – Sunwoo Park Jul 22 '14 at 14:58
0

You must change the size UITableView's contentView or frame at the creation (alloc).

This height must be the same of an UITableViewCell by default (44px) or custom.

0

Write the below 4 lines to make a table with one row. If you want to extend the content of the table just make the tableview scrollable.

tblView                     = [[UITableView alloc]initWithFrame:CGRectZero];
tblView.separatorColor      = [UIColor clearColor];
tblView.tableFooterView     = [[UIView alloc] initWithFrame:CGRectZero];    
tblView.scrollEnabled       = NO;
Nupur Sharma
  • 1,106
  • 13
  • 15