2010-01-08 58 views
1

我有一個tabBar應用程序,其ResultsNavController作爲我的TabBar的導航控制器1,視圖控制器是ResultsViewController和我的tableViewPtr連接到主窗口下的IB中的tableViewib.xib ResultsNavController/ResultsViewController /查看/的tableView。UITabBar中的UITableView reloadData問題

轉到另一個選項卡寫入PureFun.plist後,單擊結果視圖不會立即使用新的PureFun.plist重新加載表。 PureFun.plist在我檢查後成功寫入。

- (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]; 
    } 

回答

0

我知道我的問題。我的數組沒有在viewWillAppear中重新加載,所以我將viewDidLoad中的設置代碼複製到viewWillAppear並解決了它。

在附註中,UItableView問題可能是由DataSource中的數據設置引起的,[重載數據]可能不是這種情況下的主要罪魁禍首。

0

趕上你的UITabBarController當前視圖控制器的變化,請嘗試使用以下方法UITabBarControllerDelegate:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    // ... 
} 

這種方式,您可以手動觸發重新加載。有時,視圖控制器的複雜組合可能會變得複雜且難以調試,並且並不是所有的「viewWillAppear:」方法都被認爲是應該被調用的。

+0

我在哪裏添加這個?在我的RootViewController?我的結果標籤下只有IB的tableView Outlet。有沒有一種方法可以調試? – zerlphr 2010-01-08 17:24:59