首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >xcode,在退出seque时重新加载表视图

xcode,在退出seque时重新加载表视图
EN

Stack Overflow用户
提问于 2012-12-25 13:06:10
回答 2查看 5.8K关注 0票数 1

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

//**手工执行seque

代码语言:javascript
运行
复制
-(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];
}

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

代码语言:javascript
运行
复制
-(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;
        }
    }
}

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

代码语言:javascript
运行
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-28 18:52:41

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

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

代码语言:javascript
运行
复制
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

但是,它的缺点是,每次显示视图时,都会执行,即使您不一定需要重新加载数据。

2)如果您使用故事板并以iOS 6+为目标,那么当从add视图控制器返回时,您可以使用展开segue来调用视图控制器上的特定方法。有关更多信息,请参见以下问题/答案:Does anyone know what the new Exit icon is used for when editing storyboards using Xcode 4.5?

3)如果您针对的是较早版本的iOS,或者不使用情节提要,那么您可以创建一个方法的协议,只要添加了新的概要文件,就应该调用该方法,并且可以在调用该方法时重新加载该数据。这里有很多问题,这些问题涉及如何做到这一点(比如dismissModalViewController AND pass data back,它展示了如何传递数据,但是您可以做同样的事情来调用一个方法)。

票数 3
EN

Stack Overflow用户

发布于 2012-12-29 02:56:24

这是依纳芬格在上述评论中的实际回答。再次感谢。

代码语言:javascript
运行
复制
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

我花了一点时间才弄明白这一点,但这是因为您使用的是UIModalTransitionStylePartialCurl modalTransitionStyle。我想他们不会这么称呼它,因为它不会完全离开屏幕。如果您使用不同的样式(比如默认样式),那么它将调用viewWillAppear。如果您想保持这种状态,那么您可能应该使用其他方法之一。-伊纳菲格

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14030901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档