xcode,在退出序列时重新加载表视图

编程入门 行业动态 更新时间:2024-10-28 00:23:05
本文介绍了xcode,在退出序列时重新加载表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I'm trying to reload the values of a table view after exiting from a seque. The process being: I perform the seque manually from the profile selection view, add a new profile name, return to the profile selection view. Then I would like to reload the table view adding the new profile name. It is running the code fine (same code as original entry into the scene), but I can't seem to get the native methods of numberOfRowsInSection and numberOfRowsInSection to repopulate the table view. I actually have to leave the screen and reenter it before the new profile name will update. Any thoughts?

//** 手动执行序列

//** performing seque manually

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

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

//** function to load the new profile names in.

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

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

//** methods to reload the table view

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

推荐答案

这里有几个不同的选择:

You have a few different options here:

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

1) The easiest is to simply reload the table every time that the view controller is about to display its view:

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

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

The downside though, is that this will be executed every time that the view is displayed, even when you don't necessarily need to reload the data.

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

2) If you are using storyboard's and targeting iOS 6+ then you can use an unwind segue to call a specific method on your view controller when going back from the add profile view controller. For more info, see this SO question/answers: Does anyone know what the new Exit icon is used for when editing storyboards using Xcode 4.5?

3) 如果您的目标是旧版本的 iOS 或不使用故事板,那么您可以创建一个协议,该协议的方法应在添加新配置文件时调用,并且您可以在调用该方法时重新加载数据.这里有很多关于 SO 的问题,涵盖了如何做到这一点(例如 dismissModalViewController 并传回数据 显示了如何传递数据,但您可以执行相同的操作,只需调用一个方法即可).

3) If you are targeting older versions of iOS or aren't using storyboards, then you can create a protocol with a method that should be called whenever a new profile is added and you can reload the data whenever that method is called. There are lots of questions here on SO which cover how to do this (like dismissModalViewController AND pass data back which shows how to pass data, but you can do the same thing to just call a method).

这篇关于xcode,在退出序列时重新加载表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:11:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   序列   加载   xcode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!