views:

751

answers:

3

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;

}
A: 

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.

Brad Smith
you can do an NSAsset(self.tableView,@"Whoops, tableview is null"); before the [self.tableView reloadData]; to test the theory that the tableView outlet is not wired.
Brad Smith
Brad, *** Assertion failure in -[SearchView searchBarSearchButtonClicked:], SearchView.m:234*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSInternalInconsistencyException> Whoops, tableView is NullNot sure that's what I was expecting to see but it looks like the NSAssert failed.. right?? and the tableView iS null??
CocoaNewBee
A: 

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

[searchDisplayController.searchResultsTableView reloadData];
glorifiedHacker
Im not using a searchDisplayCOntroller but thanks!!! I see it under all FileOwner's but never used. Do you know where I can find some sample code to implement it?? thanks !!!
CocoaNewBee
http://developer.apple.com/iphone/library/samplecode/TableSearch/
glorifiedHacker
A: 

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.

progrmr
the viewWIllAppear is only called once. when I clicked on the tabbar and switch tabs, it never get called.
CocoaNewBee