我一直在试验 RubyMotion 和 MotionModel。浏览了整个 MotionModel 教程(https://github.com/sxross/MotionModel/wiki/Tutorial),但它不包括模型数据的序列化和持久存储。
我能够将数据序列化和存储,并且可以使用通知(重新)加载表单数据,但似乎无法在应用程序启动时正确加载模型数据。我得到一个空白的 tableView,然后当我创建模型的新实例时,所有数据都会立即弹出。我知道这很简单,但我只是没有看到它。
应用委托:
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
但没有得到任何结果。非常感谢任何指导!