UITableView reloadData problem in UITabBar
I have a tabBar application that has ResultsNavController as 1 of my tabBar's Nav Controller, the View Controller is ResultsViewController and my tableViewPtr is connected to the tableView in IB which is under Main window.xib ResultsNavController/ResultsViewController/View/tableView.
After going to another tab to write to PureFun.plist, clicking on results view doesn't reload the table instantly with the new PureFun.plist. PureFun.plist was successfully written after i checked.
- (void)viewWillAppear:(BOOL)animated {
[self.tableViewPtr reloadData];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [resultsDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ResultsCellIdentifier";
UILabel *scoreStrLabel;
UILabel *dateStrLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
dateStrLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10,5.0,200,43)] autorelease];
dateStrLabel.tag = DATESTR_TAG;
dateStrLabel.font = [UIFont systemFontOfSize:18.0];
dateStrLabel.textAlignment = UITextAlignmentLeft;
开发者_运维技巧 dateStrLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:dateStrLabel];
scoreStrLabel = [[[UILabel alloc] initWithFrame:CGRectMake(250,5.0,50,43)] autorelease];
scoreStrLabel.tag = SCORESTR_TAG;
scoreStrLabel.font = [UIFont systemFontOfSize:18.0];
scoreStrLabel.textAlignment = UITextAlignmentRight;
scoreStrLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:scoreStrLabel];
}
else {
scoreStrLabel = (UILabel *)[cell.contentView viewWithTag:SCORESTR_TAG];
dateStrLabel = (UILabel *)[cell.contentView viewWithTag:DATESTR_TAG];
}
NSDictionary *dict = [resultsDataArray objectAtIndex:indexPath.row];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"h':'mma, EEE d MMM"];
NSDate *dateTested = [dict objectForKey:@"Date"];
NSString *dateStr = [formatter stringFromDate:dateTested];
NSString *scoreStr = [dict objectForKey:@"Score"];
scoreStrLabel.text = scoreStr;
dateStrLabel.text = dateStr;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 49.1;
}
- (void)viewDidLoad {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:@"PureFun.plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:myPathDocs];
self.resultsDataArray = array;
[array release];
[super viewDidLoad];
}
To catch the changes of the current view controller in your UITabBarController, try using the following UITabBarControllerDelegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// ...
}
This way you can trigger the reload manually. Sometimes, complex combinations of view controllers might get complex and hard to debug, and not all the "viewWillAppear:" methods are called as one thinks they should be.
I know my problem. my array wasn't reloaded in the viewWillAppear, so i copied the Setup codes in viewDidLoad to viewWillAppear and that solved it.
On a side note, UItableView problem may result from the data setup within DataSource, and [reload Data] might not be the main culprit in the case.
精彩评论