1

我试图在退出序列后重新加载表视图的值。过程是:我从配置文件选择视图手动执行序列,添加一个新的配置文件名称,返回到配置文件选择视图。然后我想重新加载表格视图添加新的配置文件名称。它运行代码正常(与场景中的原始条目相同的代码),但我似乎无法获得numberOfRowsInSectionnumberOfRowsInSection的本机方法来重新填充表视图。实际上,在新的配置文件名称更新之前,我必须离开屏幕并重新输入它。有什么想法吗?


//** 手动执行序列

-(IBAction)buttonAddNewProfile:(id)sender
{
    // creating object for profile selection screen
    UIStoryboard *ProfileSelectionStoryboard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    // creating object for add new profile storyboard
    AddNewProfileViewController *addnewprofileVC=[ProfileSelectionStoryboard instantiateViewControllerWithIdentifier:@"Add New Profile"];

    // setting the transition style
    addnewprofileVC.modalTransitionStyle=UIModalTransitionStylePartialCurl;

    // performing the segue
    [self presentViewController:addnewprofileVC animated:YES completion:nil];

    // performing new table view load on return from new profile
    [self loadUsers];
}

//** 加载新配置文件名称的函数。

-(void)loadUsers
{
    // retreiving the users from the database
    SQLiteFunctions *sql = [[SQLiteFunctions alloc] init];

    // testing for successful open
    if([sql openDatabase:@"users"])
    {
        // setting query statement
        const char *query = "SELECT * FROM  users;";

        // testing for that profile name existing already
        if([sql getUserRecords:query] > 0)
        {
            // initializing array
            NSMutableArray *names = [[NSMutableArray alloc] init];

            // loop through object compling an array of user names
            for(Users *ProfileUser in sql.returnData)
            {
                // adding user name to the listview array
                [names addObject:ProfileUser.user_name];
            }

            // setting table view array to local array
            tableData = names;
        }
    }
}

//** 重新加载表格视图的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    // returning the number of rows in the table
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    // setting up the table view cells for data population
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    // testing for cell parameters
    if (cell == nil)
    {
        // setting up cloned cell parameters
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }

    // setting cell values to the array row value
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    // returning the current row label value
    return cell;
}
4

2 回答 2

3

您在这里有几个不同的选择:

1)最简单的方法是在每次视图控制器即将显示其视图时简单地重新加载表:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

但不利的一面是,每次显示视图时都会执行此操作,即使您不一定需要重新加载数据。

2)如果您使用故事板并针对 iOS 6+,那么当您从添加配置文件视图控制器返回时,您可以使用展开 segue 来调用视图控制器上的特定方法。有关更多信息,请参阅此 SO 问题/答案:有人知道使用 Xcode 4.5 编辑情节提要时使用新的退出图标是什么吗?

3) 如果您针对的是旧版本的 iOS 或不使用情节提要,那么您可以创建一个协议,其中包含一个在添加新配置文件时应该调用的方法,并且您可以在调用该方法时重新加载数据。这里有很多关于如何做到这一点的问题(比如dismissModalViewController AND pass data back,它显示了如何传递数据,但你可以做同样的事情来调用一个方法)。

于 2012-12-28T18:52:41.117 回答
0

这是 lnafzinger 在上述评论中的实际答案。再次感谢。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

我花了一点时间才弄清楚这一点,但这是因为您使用的是 UIModalTransitionStylePartialCurl modalTransitionStyle。我猜他们不会调用它,因为它并没有完全离开屏幕。如果您使用不同的样式(如默认样式),那么它将调用 viewWillAppear。如果您想坚持下去,那么您可能应该使用其他方法之一。– 尔纳夫齐格

于 2012-12-29T02:56:24.410 回答