如何在多视图 iPhone 应用程序中重置数组

发布于 2024-09-09 19:34:43 字数 2335 浏览 6 评论 0原文

我试图拥有一个表视图,该表视图会导致另一个表视图,其列表项取决于第一个视图。我已正确设置数据库,我的问题是,当我在第一页上选择一个项目并第一次进入第二个视图时,它工作正常,但当我返回第一页并再次返回第二个视图时该列表仍然包含加载了新值的旧值。有没有办法销毁数组并在每次加载第二个视图时重新创建它?我会在哪里做这个?

这是我的数组在委托中初始化的位置:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

 [self copyDatabaseIfNeeded];

 organArray = [[NSMutableArray alloc] init]; 
 procedureArray = [[NSMutableArray alloc] init];

 [Organ getInitialDataToDisplay:[self getDBPath]];

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

这是将数据添加到我的自定义类中的数组的位置:

    + (void) getDatabase:(NSString *)dbPath WithOrganID:(NSNumber *)organID
{

 RadiologyAppAppDelegate *appDelegate = (RadiologyAppAppDelegate *)[[UIApplication sharedApplication] delegate];

 if(sqlite3_open([dbPath UTF8String],&database) == SQLITE_OK){

  const char *sql = "select * from Organ";
  sqlite3_stmt *selectstmt;

  if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK){

   while (sqlite3_step(selectstmt) == SQLITE_ROW) {

    NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);

    Procedure *procedureObj = [[Procedure alloc] initWithPrimaryKey:primaryKey];
    procedureObj.procedureName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
    procedureObj.procedureID = sqlite3_column_int(selectstmt,0);

    [appDelegate.procedureArray addObject: procedureObj];

    [procedureObj release];
   }
  }
 }
 else
  sqlite3_close(database);
}

最后,这是我的视图控制器:

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

 Procedure *procedureObj = [appDelegate.procedureArray objectAtIndex:indexPath.row];
 PVCprocedureObj = appDelegate.procedureArray;

 cell.textLabel.text = procedureObj.procedureName;

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

感谢您的帮助!

I am trying to have a tableview that leads to another tableview whose list items depend on the first view. I have the database set up properly and my problem is that when I select one item on the first page and go to the second view for the first time it works fine but when I return to the first page and go back to the second view again the list still has the old values loaded with new ones. Is there a way to destroy the array and recreate it every time the second view is loaded? Where would I do this?

Here is where my array is initialized in the delegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

 [self copyDatabaseIfNeeded];

 organArray = [[NSMutableArray alloc] init]; 
 procedureArray = [[NSMutableArray alloc] init];

 [Organ getInitialDataToDisplay:[self getDBPath]];

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

Here is where the data is added to the array in my custom class:

    + (void) getDatabase:(NSString *)dbPath WithOrganID:(NSNumber *)organID
{

 RadiologyAppAppDelegate *appDelegate = (RadiologyAppAppDelegate *)[[UIApplication sharedApplication] delegate];

 if(sqlite3_open([dbPath UTF8String],&database) == SQLITE_OK){

  const char *sql = "select * from Organ";
  sqlite3_stmt *selectstmt;

  if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK){

   while (sqlite3_step(selectstmt) == SQLITE_ROW) {

    NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);

    Procedure *procedureObj = [[Procedure alloc] initWithPrimaryKey:primaryKey];
    procedureObj.procedureName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
    procedureObj.procedureID = sqlite3_column_int(selectstmt,0);

    [appDelegate.procedureArray addObject: procedureObj];

    [procedureObj release];
   }
  }
 }
 else
  sqlite3_close(database);
}

Finally, here is my view controller:

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

 Procedure *procedureObj = [appDelegate.procedureArray objectAtIndex:indexPath.row];
 PVCprocedureObj = appDelegate.procedureArray;

 cell.textLabel.text = procedureObj.procedureName;

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

Thanks for the help!

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

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

发布评论

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

评论(1

尹雨沫 2024-09-16 19:34:43

我会将数据对象(要在第二个表中显示的数据)传递给第二个控制器。我认为这是最简单的方法,而您没有展示该部分。
单击单元格时在以下位置传递对象: – tableView:didSelectRowAtIndexPath: 而不是在创建单元格时传递对象: -tableView:cellForRowAtIndexPath:
希望这有帮助

I would pass the data-object (data to be shown in the second table) to the 2nd controller. I think this is the easiest approach and you are not showing that part.
Pass the object when clicking the cell, inside: – tableView:didSelectRowAtIndexPath: and not when making the cell: -tableView:cellForRowAtIndexPath:
Hope this helps

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