【问题标题】:TableViewController not updating after editing data and saving. Only updates upon application restart编辑数据并保存后 TableViewController 未更新。仅在应用程序重新启动时更新
【发布时间】:2014-05-08 07:43:22
【问题描述】:

我目前正在制作一个应用程序,可以通过单击某些表格视图单元格然后转到单独的视图控制器来编辑存储在某些表格视图单元格中的信息。

更改完成后,按下保存按钮,并在 parse.com 上更新字段 我的问题是,一旦我回到我的主 tableViewController,表格单元格不会更新,除非我完全重新启动应用程序,此时所有内容都显示为更新。

我已经像 iOS 教程中那样调用了 [self.tableView reloadData] 方法,但它似乎不会更新。

这是我要更新的控制器的 .m 文件。

@interface ProspectsTableViewController ()

@end

@implementation ProspectsTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentUser = [PFUser currentUser];

    PFQuery *query = [PFQuery queryWithClassName:@"Prospect"];
    [query orderByAscending:@"prospectName"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error in prospect cells");
        }
        else {
            self.allProspects = objects;
            NSLog(@"%@", self.allProspects);
            [self.tableView reloadData];
        }
    }];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.allProspects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProspectCell";
    UITableViewCell *prospectCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject *prospect = [self.allProspects objectAtIndex:indexPath.row];
    prospectCell.textLabel.text = prospect[@"prospectName"];

    return prospectCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"cell pressed");
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"showProspectInfo" sender:cell];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"showProspectInfo"]) {

        ProspectInfoViewController *transferViewController = segue.destinationViewController; //prepares an instance of the prospectsTableViewController so we can pass our information over to the next view controller
        PFObject *prospect = self.allProspects[self.tableView.indexPathForSelectedRow.row]; //selects the information from the prospect at the cell tapped on
        transferViewController.name = prospect[@"prospectName"];
        transferViewController.phone = prospect[@"phoneNumber"];
        transferViewController.email = prospect[@"email"];
        transferViewController.objectId = [prospect objectId];
    }
}


@end

有什么想法吗?我一直在努力解决这个问题已经有一段时间了

提前致谢!

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    回到表格视图控制器后,您似乎没有调用[self.tableView reloadData];。尝试将其添加到表视图控制器的实现中,以在视图控制器即将出现时刷新表视图:

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

    【讨论】:

    • 这非常有效,但事实证明我还必须在 viewWillAppear 方法中从 parse.com 重新加载我的数据。感谢您的帮助。
    • 没问题!尽管我发现您还必须从 Parse 重新加载数据很有趣……我认为对 PFObjects 所做的更改是立即的(即本地的)。哦,很高兴这有帮助!
    【解决方案2】:

    viewDidAppear 中尝试reloadData

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

    【讨论】:

    • 这与我在您之前 1 分钟发布的内容完全相同。
    • @DrBeardface 如果您知道viewDidAppearviewWillAppear 之间的区别,这不一样
    • @purrrminator 啊,你是对的。我错过了。我的错。
    猜你喜欢
    • 1970-01-01
    • 2013-09-15
    • 2012-05-30
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多