Search code examples
objective-cioscocoa-touchuitableviewviewwillappear

table does not refresh after i press back and select another option. always the same table as the first selection


I have a list of Patient objects in my main page, which is a table view. When I click on one of the rows, it will go to another page which shows two options pertaining to that particular patient, option 1 being "Patient Info", and option 2 being a list of all admission dates of the particular patient selected.

I have some trouble with the second option -- the table of admission dates does not seem to refresh when I return back to the main page and select another patient. The admission dates table always shows the the admission dates of the first patient which I have selected. I really do not know where is the mistake.

I used the viewWillAppear method because viewDidLoad seems to only be called once; I proved that using NSLog(_nric); -- it only prints once. I suspect it's the table view method which is giving me the problem. Can someone please give me some advice on my second method below?

- (void)viewWillAppear:(BOOL)animated {
    _listOfPatientsAdmInfoOptions = [[NSMutableArray alloc] init];



    for(PatientAdmInfo *patientAdmInfo1 in [[PatientDatabase database] patientAdminList:_nric]){

        [ _listOfPatientsAdmInfoOptions addObject:patientAdmInfo1];
        NSLog(_nric); 

    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    _patientAdmInfo = [[PatientAdmInfo alloc] init ];


    _patientAdmInfo = [_listOfPatientsAdmInfoOptions objectAtIndex:indexPath.row];

    cell.textLabel.text = _patientAdmInfo.pDiagnosis;
    //cell.detailTextLabel.text = patientAdmInfo.pDiagnosis, patientAdmInfo.sDiagnosis;


    return cell;
}

Solution

  • Call reloadData method on your table Instance after updating your array in - (void)viewWillAppear:.

    [myTabelView reloadData];
    

    And also not forget to call [super viewWillAppear:animated]; in viewWillAppear method.

    So your viewWillAppear method should be ..

    -

    (void)viewWillAppear:(BOOL)animated {
    
         [super viewWillAppear:animated];
        _listOfPatientsAdmInfoOptions = [[NSMutableArray alloc] init];
    
    
    
        for(PatientAdmInfo *patientAdmInfo1 in [[PatientDatabase database] patientAdminList:_nric]){
    
            [ _listOfPatientsAdmInfoOptions addObject:patientAdmInfo1];
            NSLog(_nric); 
    
        }
       [myTabelView reloadData];
    }