在app start上将MotionModel反序列化为TableView

时间:2013-06-26 02:05:01

标签: ios rubymotion motionmodel

我一直在尝试使用RubyMotion和MotionModel。完成了整个MotionModel教程(https://github.com/sxross/MotionModel/wiki/Tutorial),但它不包括模型数据的序列化和持久存储。

我能够将数据序列化并存储,并且可以使用通知(重新)加载表单数据,但似乎无法在启动应用程序时正确加载模型数据。我得到一个空白的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中嵌入了该方法代码的各种再现,但没有随处获得。非常感谢任何指导!

1 个答案:

答案 0 :(得分:0)

想出来。需要将Entry.deserialize_from_file('entries.dat')移至EntryController viewDidLoad方法。成功:

  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