我按后退并选择另一个选项后,表格不会刷新。始终与第一个选择相同的表

发布于 2024-11-15 22:24:57 字数 1407 浏览 4 评论 0原文

我的主页中有一个 Patient 对象列表,它是一个表格视图。当我单击其中一行时,它将转到另一页,其中显示与该特定患者相关的两个选项,选项 1 是“患者信息”,选项 2 是所选特定患者的所有入院日期的列表。

我对第二个选项遇到了一些麻烦——当我返回主页并选择另一位患者时,入院日期表似乎没有刷新。入院日期表始终显示我选择的第一位患者的入院日期。我实在不知道错在哪里。

我使用了 viewWillAppear 方法,因为 viewDidLoad 似乎只被调用一次;我证明了使用 NSLog(_nric); ——它只打印一次。我怀疑是表视图方法给我带来了问题。有人可以给我一些关于下面第二种方法的建议吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2024-11-22 22:24:57

在 - (void)viewWillAppear: 中更新数组后,在表实例上调用 reloadData 方法。

[myTabelView reloadData];

并且不要忘记在 viewWillAppear 方法中调用 [super viewWillAppear:animated];

所以你的 viewWillAppear 方法应该是..

-

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

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];
}
野心澎湃 2024-11-22 22:24:57

这是因为您的 _patentAdmInfo 对象是一个实例变量。表中的每个单元格都在查看同一对象以获取其数据。您的 cellForRowAtIndexPath: 方法应如下所示:

PatientAdminInfo *tempPatient = [_listOfPatientsAdmInfoOptions objectAtIndex:indexPath.row];
cell.textLabel.text = tempPatient.pDiagnosis;

这将确保每个单元格都在查看适当的 PatientAdminInfo 对象。

It's because your _patientAdmInfo object is an instance variable. Every cell in your table is looking at the same object to get its data. Your cellForRowAtIndexPath: method should look like this:

PatientAdminInfo *tempPatient = [_listOfPatientsAdmInfoOptions objectAtIndex:indexPath.row];
cell.textLabel.text = tempPatient.pDiagnosis;

this will ensure that every cell is looking at the appropriate PatientAdminInfo object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文