gpt4 book ai didi

IOS/objective-c : Issue with "real time" save into coreData

转载 作者:行者123 更新时间:2023-11-29 00:29:26 25 4
gpt4 key购买 nike

我的应用程序基于 coreData,使用 mapKit 和点注释。该应用程序运行时具有所有功能;主要问题是,当我在此文本字段中输入新位置时,这个新位置不会像应有的那样出现在 TableView 中。仅当我重新启动应用程序时,尽管有表格 View 重新加载指令,表格才会刷新并且新位置可用。有谁知道为什么会发生这种情况以及我该如何解决它?谢谢。这表格 View 的代码:

@interface TableViewController ()

@property (strong) NSMutableArray *lisbonSpots;


@end

@implementation TableTableViewController

- (IBAction)delete:(UIBarButtonItem *)sender {
self.tableView.editing = !self.tableView.editing;
}

- (void)viewDidLoad {
[super viewDidLoad];

//_bar=[Spot spotType:@"bar"];


self.tableView.dataSource = self;

_lisbonSpots = [[Spot allSpots]mutableCopy];
NSLog(@"The Core Spots are: %@", _lisbonSpots);

[self.tableView reloadData];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return 0;
}

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

return self.lisbonSpots.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSManagedObject *ls = [self.lisbonSpots objectAtIndex:indexPath.row];
NSLog(@"Table Spots are: %@", ls);
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [ls valueForKey:@"name"]]];
cell.imageView.image = [UIImage imageNamed:@"city.jpg"];
return cell;
}

最佳答案

我认为您遇到的问题是由于在 viewDidLoad 中调用 reloadData 造成的。

Only when I restart the app, the table is refreshed and the new spot is available despite having the tableview reload instruction

viewDidLoad 应该用于初始设置工作或“一次性”仅在设置 View Controller 时工作。它很可能在每次应用程序启动时只调用一次。有关 View Controller 生命周期的更多信息,请参阅此答案:Looking to understand the iOS UIViewController lifecycle .

尝试将您的 reloadData 调用移动到 viewDidAppear,如下所示:

- (void)viewDidAppear {
[super viewDidAppear];
_lisbonSpots = [[Spot allSpots]mutableCopy];

[self.tableView reloadData];
}

关于IOS/objective-c : Issue with "real time" save into coreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com