2013-06-26 1 views
0

Я экспериментировал с RubyMotion и MotionModel. Был проведен весь учебник MotionModel (https://github.com/sxross/MotionModel/wiki/Tutorial), но он не охватывает сериализацию и постоянное хранение данных модели.Deserializing MotionModel в TableView при запуске приложения

Мне удалось получить данные, которые были сериализованы и сохранены, и могут (повторно) загружать данные формы с помощью уведомлений, но не могут правильно загружать данные модели при запуске приложения. Я получаю пустой tableView, а затем, когда я создаю новый экземпляр модели, все данные появляются сразу. Я знаю, что это просто, но я просто этого не вижу.

AppDelegate:

class AppDelegate 
    attr_reader :window 

    def application(application, didFinishLaunchingWithOptions:launchOptions) 

    controller = EntryController.alloc.init 

    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 
    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(controller) 
    @window.rootViewController.wantsFullScreenLayout = true 
    @window.makeKeyAndVisible 

    Entry.deserialize_from_file('entries.dat') 

    true 
    end 
end 

Контроллер:

MotionModelDataDidChangeNotification = 'MotionModelDataDidChangeNotification' 

class EntryController < UIViewController 

    def viewDidLoad 
    super 

    self.title = "Entries" 

    @entry_model_change_observer = App.notification_center.observe MotionModelDataDidChangeNotification do |notification| 
     if notification.object.is_a?(Entry) 
     reload_data 
     end 
    end 

    #create and add tableview 
    @table = UITableView.alloc.initWithFrame([[0,0],[320,480]]) 
    @table.dataSource = @table.delegate = self 
    self.view.addSubview @table 

    ##adds top right "new" button 
    right_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemAdd, target:self, action:"new_entry") 
    self.navigationItem.rightBarButtonItem = right_button 

    end 

    def viewWillDisappear(animated) 
    App.notification_center.unobserve @task_model_change_observer 
    end 

    def reload_data 
    @entries = Entry.all 
    @table.reloadData 
    end 

    def new_entry  
    Entry.create :details => "New entry" 
    end 

    def numberOfSectionsInTableView(view) 
    1 
    end 

    def tableView(tableView, numberOfRowsInSection: section) 
    Entry.count 
    end 

    def tableView(tableView, cellForRowAtIndexPath: indexPath) 
    @reuseIdentifier ||= "entry_cell" 
    cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier) || begin 
     UITableViewCell.alloc.initWithStyle UITableViewCellStyleSubtitle, reuseIdentifier:@reuseIdentifier 
    end 

    entry = @entries[indexPath.row] 

    cell.textLabel.text = entry.details 
    cell.detailTextLabel.text = entry.details 
    cell 
    end 


    def tableView(tableView, didSelectRowAtIndexPath:indexPath) 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    end 
end 

Я пытался дозвониться reload_data и внедренных различных выдач коды этого метода внутри viewDidLoad, но не получить в любом месте с ним. Любое руководство очень ценится!

ответ

0

Выяснил это. Необходимо переместить Entry.deserialize_from_file('entries.dat') в метод EntryControllerviewDidLoad. Изготовление его:

def viewDidLoad 
    super 

    self.title = "Entries" 

    Entry.deserialize_from_file('entries.dat') 
    @entries = Entry.all 

    @entry_model_change_observer = App.notification_center.observe MotionModelDataDidChangeNotification do |notification| 
     if notification.object.is_a?(Entry) 
     reload_data 
     end 
    end 

    #create and add tableview 
    @table = UITableView.alloc.initWithFrame([[0,0],[320,480]]) 
    @table.dataSource = @table.delegate = self 
    self.view.addSubview @table 

    ##adds top right "new" button 
    right_button = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemAdd, target:self, action:"new_entry") 
    self.navigationItem.rightBarButtonItem = right_button 

    end