【发布时间】:2015-11-19 14:09:26
【问题描述】:
我有一个我的 tableViewDataSource 依赖的 RealmObject 模型。 cellForRowAtIndexPath1 方法可以正常访问属性,但 didSelectRowAtIndexPath 从 Realm 对象中获取所有空属性。我猜这与传递持久的 Realm 对象有关,但我不确定在哪里修复它。
//viewController...
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let realm = try! Realm()
let books = realm.objects(Book)
viewModel = BookListViewModel(books: Array(books), onSelection: onSelection, onDeleteFailure: onDeleteFailure)
}
//end viewController
extension BookListViewModel: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(BookTableViewCell.nibName, forIndexPath: indexPath) as! BookTableViewCell
cell.bind(bookCellViewModels[indexPath.row])
// NOTE:
// bookCellViewModels[indexPath.row].book all properties are valid strings
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bookCellViewModels.count
}
}
extension BookListViewModel: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let book = bookCellViewModels[indexPath.row].book
// NOTE:
// all book properties are empty string or nil
onSelection(book)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
【问题讨论】: