2010-01-08 77 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