2017-03-16 5 views
0

「ストア」(Realmオブジェクト)のリストを表示する埋め込みUITableViewを持つ1つのビューアプリケーションがあります。デフォルトでは、すべてのStoreオブジェクトのテーブルビューが設定されます。ユーザーが結果を絞りたい場合は、MasterVCのテキストフィールドの任意の組み合わせを使用して結果を絞り込むことができます。彼らが検索を打つと、 TableViewに 'filtered' Realmオブジェクトがあります。UITableViewの最初の行 '0'はテーブルの再読み込み時に更新されません。

何作品:

  • 移入のUITableViewレルムからのオブジェクトと。
  • MasterVCのテキストフィールドエントリを使用して新しいレルムエントリを作成し、ResultsVCのテーブルを再作成します。
  • テーブル/とレルムオブジェクトのオブジェクトを削除するにはスワイプします。

どの作品の並べ替え:

  • ユーザーが検索語を入力する場合は、「フィルタ」レルムオブジェクト(店舗)とテーブルを再作成します。これにより、結果が正しくリロードされ、結果の数が返されます。 ただしTableViewの最初のセル(0)です。常に正確に同じです。決して更新されません。検索で20の結果が返された場合、1-18の行が正しく表示されます。行0は静的であり、テキストは決して変更されません。理由は何ですか?ここで

結果表ビューコントローラ

class ResultsVC: UITableViewController { 

    // data source 
    var stores: Results<Store> = { 
     let realm = try! Realm() 
     return realm.objects(Store.self) 
    }() 

    var token: NotificationToken? 

    ... 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return stores.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! ResultsCustomViewCell 

     let stores = realm.objects(Store.self) 
     let currentStore = stores[indexPath.row] 
     cell.storeNumber.text = "#\(currentStore.storeNumber)" 
     cell.storeName.text = "\"\(currentStore.storeName)\"" 
     return cell 
    } 
} 

は私がからResultsVCにアクセスMasterVC

マスター・ビュー・コントローラ

class MasterViewController: UIViewController { 
    ... 
    @IBAction func searchDatabase(_ sender: Any) { 
     let CVC = childViewControllers.first as! UINavigationController 
     let resultVC = CVC.viewControllers[0] as? ResultsVC 
     result.stores = stores.filter("address = '1234 Blue Street'") 
     result.tableView.reloadData() 
    } 
    ... 
} 
てる方法です
+0

インデックスパスの行のセルにブレークポイントを配置すると、どの値が行0に表示されますか?新しい値ですか、実際のセルフィールドは更新されていません(何とか)? – DonMag

+0

'func tableView(_ tableView:UITableView、cellForRowAt indexPath:IndexPath) - > UITableViewCell'でコードを共有する必要があります。それともあなた自身で調べてください。現在提供している情報では不十分です。 –

+0

@OwenZhaoそれだった。それを100回見落とした。間違って元の「店舗」変数を上書きします。 'let stores = realm.objects(Store.self)'を削除して、そうする必要があります。あなたが答えとしてそれを受け入れるように追加したい場合。ありがとう。 –

答えて

1

私は上からorigを上書きしていた重複変数がありました。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! ResultsCustomViewCell 

    let stores = realm.objects(Store.self) // <- OVERWRITING ORIGINAL // 
    let currentStore = stores[indexPath.row] 
    cell.storeNumber.text = "#\(currentStore.storeNumber)" 
    cell.storeName.text = "\"\(currentStore.storeName)\"" 
    return cell 
} 
関連する問題