1
votes

I've been debugging my iphone app and found something interesting.

I have a UIViewControllers with TabBarcontroller( 6 tabs). Each tab is a UIViewController and it has a UITtableview. The viewDidLoad works and brings the initial data. On of the UITableView has a search bar. After the user touchs presses search some magic happens and I get an array with data. I cant see the new data in the tableview and the [tableView reloadData] has no effect outside viewDidLoad (first time).

I can see the array holding the data and the dataSource is set to self. Yet, no displaying of data!

so I 've tried [self.tableView reloadData] & [self.tableView setNeedsDisplay]

Funny enough, the new data is not being displayed. However if I move the table up or down the cellForRowAtIndexPath is being fired and the first row shows data.

can anyone shed some light on this mystery???

if there a [self.view refreshscreen] ??

-(void) viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
    [super viewWillAppear:animated];    
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar2 {   
    [self searchForFullNames];
        //NSAssert(tableView, @"Whoops, tableView is Null");
    [tableView reloadData];
        // hide keyboard
    [searchBar2 resignFirstResponder];
}
- (void)viewDidLoad {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"search" ofType:@"plist"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];  
    self.names = dict;
    [dict release];
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.keys = array;
    isSearchOn = NO;
    canSelectRow = NO;

    self.tableView.scrollEnabled = NO;

    [super viewDidLoad];
}



- (void) searchForFullNames {

    self.listData = nil;
    self.names = nil;
    self.keys = nil;

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]  init];
    NSMutableArray *person = [[NSMutableArray alloc] init];

    [person addObject:@"Doe, John"];
    [person addObject:@"11234"];    
    [person addObject:@"11/22/75"]; 

    [dict setObject:person forKey:@"Person 1"];

    person = [[NSMutableArray alloc] init];

    [person addObject:@"Doe, Mary"];
    [person addObject:@"4321"]; 
    [person addObject:@"11/22/85"]; 

    [dict setObject:person forKey:@"Person 2"];

    person = [[NSMutableArray alloc] init];

    [person addObject:@"Doe, John"];
    [person addObject:@"336655"];   
    [person addObject:@"10/22/84"]; 

    [dict setObject:person forKey:@"Person 3"];

    self.names = dict;

    [dict release];
        NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.keys = array;

    isSearchOn = NO;
    canSelectRow = YES;

}
4
Usually, when you call a super's method, you put that call at the beginning of the method that you are overriding (dealloc is a notable exception). Call [super viewDidLoad] before you perform your own setup; otherwise you may inadvertently wind up clobbering your own setup. The same goes for your implementation of viewWillAppear. - glorifiedHacker
How about you post the code for [self searchForFullNames]? - glorifiedHacker
sorry about that.. I kept forgetting to add the code!! :) the issue is that it seems that searching is designed for data sitting in memory. I want to retrieve data from a web service given the data entered in the search bar & populate the table view after removing the previous data currently in memory.. - CocoaNewBee

4 Answers

7
votes

UISearchDisplayController has its own tableView property called searchResultsTableView. If you are using a searchDisplayController, try using:

[searchDisplayController.searchResultsTableView reloadData];
1
votes

The behavior you are describing (with the new data not being shown until scrolling) is exactly what I would expect to happen if you did not call reloadData on the table view. I would set a few breakpoints and make sure that the reloadData is really being called, on the right UITableView, and that cellForRowAtIndexPath is called, as a result of the reloadData.

One guess is that your self.tableView IBOutlet is not wired up, and that self.tableView is NULL. It is possible that the tableview Delegate and Datasource are wired up, but not the tableView itself.

1
votes
[self.searchDisplayController.searchResultsTableView reloadData];
0
votes

viewDidLoad only gets called once when the view controller is loaded. You probably want to update the table every time the view gets displayed (when you switch tabs for example), put your reloadData call in viewWillAppear instead.