0
我有一個嵌入式UITableView的視圖應用程序,顯示「商店」(Realm對象)的列表。默認情況下,我填充所有Store對象的表視圖。如果用戶希望通過使用MasterVC中的任何文本字段組合來縮小結果,他們可以這樣做。當他們點擊搜索 - 只需更新 TableView與'過濾'領域對象。UITableView第一行'0'不會更新表重裝 - 所有其他人呢?
什麼工作:
- 填充的UITableView從境界的對象。
- 通過MasterVC中的文本字段條目創建新的領域條目,並在ResultsVC中重新填充表格。
- 滑動以刪除表格/和Realm對象上的對象。
什麼樣的作品:
- 如果用戶輸入然後搜索術語「過濾器」的境界對象(店)和重新填充表。這正確地重新加載並返回結果的數量。 但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行的值是什麼?他們是新的值,但實際的單元格字段沒有被更新(以某種方式)? – DonMag
你還應該在'func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell'上分享你的代碼。或者自己調查一下。你目前提供的信息是不夠的。 –
@OwenZhao就是這樣。忽略了它一百次。錯誤地覆蓋原來的「商店」變量。刪除了'let stores = realm.objects(Store.self)'並且像它應該那樣工作。如果你想添加答案,我接受它。謝謝。 –