1

I want to load the search display controller's table view with all the data in the data source before the user begins the search.

I used

[self.searchDisplayController.searchResultsTableView reloadData];

in viewDidLoad but the UITableViewDataSource delegate method

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

does not get called. My table stays empty. I manually filled the data source before reloading the table view (and verified this in debug mode), so there is data to populate cells with.

I know I can do this using another table view and achieve exactly same results visually but I want to know if only the search display controller's table view can be used.

Code:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;

    static NSString *cellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    NSString *category = [searchResults objectAtIndex:[indexPath row]];
    cell.textLabel.text = category;

    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;


    return cell;
}
Shuaib
  • 779
  • 2
  • 13
  • 47

2 Answers2

1

You might want to have a look at this SO question. Never tried it though.

Tell me if it helped ;-)

EDIT :

After your comment, I tried the answer, and it worked fine for me (still got the dimmed view, as he says in his answer. But did not try to remove it).

Here is the code I tried in an application of mine :

- (void)viewDidLoad {
    [super viewDidLoad];
    _places = @[ [[RHPlace alloc] initWithName:@"name" address:nil distance:nil andFoursquareUID:@"plop"] ];
    [self.searchDisplayController.searchResultsTableView reloadData];
    CGRect testFrame = CGRectMake(0, 20, self.searchDisplayController.searchBar.frame.size.width, 100);
    self.searchDisplayController.searchResultsTableView.frame = testFrame;
    [self.searchDisplayController.searchBar.superview addSubview:self.searchDisplayController.searchResultsTableView];
}

And here is the result I got : http://cl.ly/image/0k0V3J3H1G1h

So if it doesn't work for you, I guess you have another bug somewhere else.

Community
  • 1
  • 1
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
  • it did not work.....i dont know why....the theory was and the cause of the problem was explained correctly in the post you linked.... – Shuaib Aug 09 '13 at 04:28
  • It does not work for me....now I am doing it **without** the search display controller and I am getting the results – Shuaib Aug 18 '13 at 07:43
0

may be you add data in 'searchResults' array while delegate method...

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

in your case you need whole data in search display controller's table view without search may be not call above delegate method and your array has no data... so you need to add data in 'searchResults' array before reload of search display controller's table view.

[self.searchDisplayController.searchResultsTableView reloadData];
Dafda
  • 202
  • 1
  • 3
  • i manually added data in searchResults array then reloaded the tableView. i verified in debug mode that searchResults contains data – Shuaib Aug 07 '13 at 06:08