我有一个tabBar应用程序,它将ResultsNavController作为我的tabBar的Nav控制器中的一个,View Controller是ResultsViewController,我的tableViewPtr连接到IB中的tableView,它位于主窗口下.xxib ResultsNavController / ResultsViewController / View / tableView。 / p>
转到另一个选项卡以写入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 :(得分:0)
要捕获UITabBarController中当前视图控制器的更改,请尝试使用以下UITabBarControllerDelegate方法:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// ...
}
这样您就可以手动触发重新加载。有时,视图控制器的复杂组合可能变得复杂且难以调试,并且并非所有“viewWillAppear:”方法都被调用,因为人们认为它们应该是。
答案 1 :(得分:0)
我知道我的问题。我的数组没有在viewWillAppear中重新加载,所以我将viewDidLoad中的安装代码复制到viewWillAppear并解决了它。
另一方面,UItableView问题可能是由DataSource中的数据设置引起的,[重新加载数据]可能不是案例中的主要罪魁祸首。