我的应用程序是基于Coredata,使用MAPKIT和点注释。该应用程序以其所有功能运行;主要问题是,当我在此文本字段上输入一个新位置时,该新点不会像应有的那样出现在表视图中。只有当我重新启动该应用程序时,尽管有TableView Reload指令,但仍可以刷新表,并且可以使用新的位置。有人知道为什么会发生这种情况以及如何解决?谢谢。这表视图的代码:
@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
。
仅在我重新启动应用程序时,表格进行了刷新,尽管有tableview reload说明
viewDidLoad
应用于初始设置工作,或仅在设置视图控制器时进行"一次性"。每个应用程序发布一次很可能只调用一次。有关查看控制器生命周期的更多信息,请参阅以下答案:要了解iOS uiviewController LifeCycle。
尝试将reloadData
调用到viewDidAppear
:
- (void)viewDidAppear {
[super viewDidAppear];
_lisbonSpots = [[Spot allSpots]mutableCopy];
[self.tableView reloadData];
}
尝试使用nsfetchedresultscontroller获取数据:在初始化时调用的单个方法中遵循代码,为nsfetchedresultscontroller *fetchedController创建一个实例变量;
NSManagedObjectContext *context = <#Managed object context#>;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Configure the request's entity, and optionally its predicate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"<#Sort key#>" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:@"<#Cache name#>"];
[fetchRequest release];
NSError *error;
BOOL success = [controller performFetch:&error];
和tableView委托方法,在为您插入或删除后,获取ressultscontroller进行了提取过程。(代码来自Apple文档)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[<#Fetched results controller#> sections] count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection (NSInteger)section {
if ([[<#Fetched results controller#> sections] count] > 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
} else
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = <#Get the cell#>; // Your chase [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath];
// Configure the cell with data from the managed object.
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([[<#Fetched results controller#> sections] count] > 0) {
id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
return [sectionInfo name];
} else
return nil;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [<#Fetched results controller#> sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [<#Fetched results controller#> sectionForSectionIndexTitle:title atIndex:index];
}
希望它能帮助您!