2017-02-16 55 views
0

我的應用程序是基於coreData的,使用mapKit和現場註釋。該應用程序運行所有功能;主要的問題是,當我在這個文本字段上輸入一個新的位置時,這個新點並不像它應該顯示在表視圖中。只有當我重新啓動應用程序時,表格纔會刷新,儘管有tableview重新加載指令,新點仍然可用。有沒有人有線索爲什麼會發生這種情況,我怎麼能解決它? 謝謝。爲表視圖 代碼:IOS/Objective-c:問題與「實時」保存到coreData

@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; 
} 
+0

有你在主線程中調用tableview.reloadData? –

+0

顯示一些代碼,所以我們可以幫助 –

+0

我有TableViewController(didLoad) – FuManchu

回答

1

我相信你遇到的問題是由於在viewDidLoad內調用reloadData

只有當我重新啓動應用程序,該表將刷新,新的現貨可儘管擁有實現代碼如下重裝指令

viewDidLoad應當用於初始設置工作或「一次性」只在設置視圖控制器時工作。它很可能只在每個應用程序啓動時被稱爲ONCE。有關視圖控制器生命週期的更多信息,請參閱此答案:Looking to understand the iOS UIViewController lifecycle

試試你的reloadData呼叫轉移到viewDidAppear這樣的:

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

    [self.tableView reloadData]; 
} 
+0

它沒有工作,問題仍然存在......但其他奇怪的是(沒有解釋)引腳出現在同一個應用程序會話的地圖上,但沒有在表中的名稱......對不起, – FuManchu

+0

你的回答是對的,但是缺少一行:_lisbonSpots = [[Spot allSpots] mutableCopy]; 該方法必須包含源數據。雖然,信用是你的,謝謝:) – FuManchu

0

嘗試獲取與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委託方法,fetchedResultsController在爲您插入或刪除後執行提取過程。 (代碼從Apple Docs

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

希望它可以幫助你!

+0

感謝您的回答,但我從來沒有使用過這個控制器,而且我在一個緊湊的平面上......它必須是一個解決方案 – FuManchu

+0

試一試。值得的!它比看起來更簡單。只要嘗試實現NSFetchedResultscontroller的聲明作爲windowDidLoad之後調用的函數,其餘部分很簡單。 – voltae