0

这是我第一次提出问题并发布代码,所以我希望我已经包含了所有必要的内容。

在我的其他几个应用程序中,我已经能够成功地在 UITableView 中重新加载数据,但由于某种原因,我无法让它在这里工作。

我正在使用 navigationController 并在 UITableView 中向下钻取几个级别,直到加载一个新类,该类现在只有两个按钮,可以在 2 个相似的 plist 文件之间切换,所以我可以知道 tableView 实际上正在重新加载(Data.plist & Data2.列表)

这最终将成为一种时间表类型的应用程序,其中列出了各个工作,并且用户(司机)将使用 In/Out 按钮打一个时钟。现在,我想要向下钻取并单击加载另一个 plist 的按钮,然后返回以显示新的 plist 数据已加载。我的问题是我根本无法让 tableView 重新加载。我尝试将 [self.tableView reloadData] 和 [myTableView reloadData](我也通过 IB 连接)的不同变体放在各处,但它们都不起作用。我目前正在从 detailViewController(按钮所在的位置)调用 rootViewController(tableView 所在的位置)中的一个方法,并且当没有使用 navigationController 时,该基本过程在其他应用程序中适用于我。在这个应用程序中,navigationController 似乎让我失望了。我希望可以找到一个简单的解决方案。到目前为止,我的代码如下所示:

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

根视图控制器.m

- (void)viewDidLoad {    
    [super viewDidLoad];
    //THIS ESTABLISHES WHICH PLIST TO LOAD BASED ON THE BUTTON CLICKED
    plistToUse = [[NSUserDefaults standardUserDefaults] objectForKey:@"plistToUse"];
    if (plistToUse == @"Data.plist") {
        NSString *Path = [[NSBundle mainBundle] bundlePath];
        NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"];
        NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
        self.data = tempDict;
        [tempDict release];
    } else if (plistToUse == @"Data2.plist") {
        NSString *Path = [[NSBundle mainBundle] bundlePath];
        NSString *DataPath = [Path stringByAppendingPathComponent:@"Data2.plist"];
        NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
        self.data = tempDict;
        [tempDict release];
    } else  {
        NSString *Path = [[NSBundle mainBundle] bundlePath];
        NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"];
        NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
        self.data = tempDict;
        [tempDict release];
    }

    if(CurrentLevel == 0) {            
        NSArray *tempArray = [[NSArray alloc] init];
        self.tableDataSource = tempArray;
        [tempArray release];            
        self.tableDataSource = [self.data objectForKey:@"Rows"];            
        self.navigationItem.title = @"Choose Driver";
    } else if (CurrentLevel == 1) {
        self.navigationItem.title = @"Choose Day";
    } else if (CurrentLevel == 2) {
        self.navigationItem.title = @"Choose Job";
    } else if (CurrentLevel == 3) {
        self.navigationItem.title = @"Job Details";
    } else {
        self.navigationItem.title = CurrentTitle;
    }         
}


-(void)update {
    dvController.labelHelper.text = @"UPDATED"; //USED TO SEE IF A LABEL IN THE BUTTON CLASS WILL UPDATE        
    NSArray *tempArray = [[NSArray alloc] init];
    self.tableDataSource = tempArray;
    [tempArray release];        
    self.tableDataSource = [self.data objectForKey:@"Rows"];
    self.navigationController.navigationItem.title = @"Choose Driver";
    self.navigationController.title = @"THE TITLE";        
    [myTableView reloadData];    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    unsortedIndex=1;
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.tableDataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }


    dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    cell.textLabel.text = [dictionary objectForKey:@"Title"];

    NSArray *Children = [dictionary objectForKey:@"Children"];
    if ([Children count] == 0) {
        Titles = [dictionary objectForKey:@"Title"];
        in1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"InTime1"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", in1];
    }
    in1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"InTime1"];
    out1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"OutTime1"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%@", in1, out1];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
    NSArray *Children = [dictionary objectForKey:@"Children"];
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    if([Children count] == 0) {     
        [self.navigationController pushViewController:dvController animated:YES];
        dvController.labelSiteName.text = [dictionary objectForKey:@"Company"];
        dvController.labelSiteAddress.text = [dictionary objectForKey:@"Address"];
        dvController.labelSiteNotes.text = [dictionary objectForKey:@"Notes"];
        [dvController.mapView setMapType:MKMapTypeStandard];
        [dvController.mapView setZoomEnabled:YES];
        [dvController.mapView setScrollEnabled:YES];
        dvController.mapView.showsUserLocation = YES;
        [dvController release];
    }
    else {        
        RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
        rvController.CurrentLevel += 1;
        rvController.CurrentTitle = [dictionary objectForKey:@"Title"];
        [self.navigationController pushViewController:rvController animated:YES];
        rvController.tableDataSource = Children;
        [rvController release];
    }

}

DetailViewController.m 这两个按钮应该使用 Data.plist 或 Data2.plist 重新加载 tableView

-(IBAction)getInTime1:(id)sender {   
    plistToUse = @"Data.plist";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:plistToUse forKey:@"plistToUse"];
    [defaults synchronize];
    [rvController update];
}


-(IBAction)getOutTime1:(id)sender {
    plistToUse = @"Data2.plist";
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:plistToUse forKey:@"plistToUse"];
    [defaults synchronize];
    [rvController update];
}

感谢您提供的任何帮助。

4

1 回答 1

1

将您从plist代码中获取的数据添加到您的viewDidAppearorviewWillAppear方法中,以便每次视图出现时,数据都会从 plist 加载。

另请注意,您的数组仅分配一次。

这对你有用。

于 2012-09-18T06:00:20.073 回答