【问题标题】:table does not refresh after i press back and select another option. always the same table as the first selection在我按下并选择另一个选项后,表格不会刷新。始终与第一个选择相同的表
【发布时间】:2011-06-19 15:25:17
【问题描述】:

我的主页中有一个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;
}

【问题讨论】:

    标签: objective-c ios cocoa-touch uitableview viewwillappear


    【解决方案1】:

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

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

    【讨论】:

    • @jhaliya 非常感谢您的及时回复.. 我试过 [myTableView reloadData];但是有一个错误说明“未知接收者myTableView”....你知道可能是什么问题吗? =(
    • 使用您的 UITabelView 对象的名称并删除 _patientAdmInfo = [[PatientAdmInfo alloc] init ]; 它与代码无关..
    • 对不起,我是个菜鸟……但我在这里真的很新……我没有声明任何 UITableView 对象……我该如何找到我的 UITableView 对象的名称
    • 您的视图控制器的基类名称是什么?
    • 感谢贾利亚!我这样做了[self.tableVIew reloadData];它奏效了.. 非常感谢.. 你是救生员 =)
    【解决方案2】:

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

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

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

    【讨论】:

    • .. 感谢您的及时回复。我做了你说..但表格仍然填充与以前相同的信息......任何其他线索可能是错误的? =(
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2013-03-21
    • 1970-01-01
    • 2014-01-30
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多