UITableView 在 segmentedControlChanged 上没有改变
UITableView Not Changing on segmentedControlChanged
我正在使用 Realm 将数据加载到我的 UITableView 中,并且我已经将 UISegmentedControl 设置为导航中的标题;但是当 segmentedControlChanged 被触发时,我的 tableView 中没有任何变化。
var productViewSegmentedControl: UISegmentedControl? = nil
let realm = try! Realm()
var allProducts : Results<Product>?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if allProducts == nil {
allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
}
if productViewSegmentedControl == nil {
let segmentedControlItems = ["List", "Brands", "Categories"]
productViewSegmentedControl = UISegmentedControl(items: segmentedControlItems)
productViewSegmentedControl?.selectedSegmentIndex = 0
self.navigationItem.titleView = productViewSegmentedControl
productViewSegmentedControl?.addTarget(self, action: #selector(OrderFormViewController.segmentedControlChanged(_:)), for:.allEvents)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func segmentedControlChanged(_ segControl: UISegmentedControl){
switch segControl.selectedSegmentIndex{
case 0:
_ = allProducts?.sorted(byKeyPath: "basedescription")
tableView.reloadData()
case 1:
_ = allProducts?.sorted(byKeyPath: "itembrand")
tableView.reloadData()
case 2:
_ = allProducts?.sorted(byKeyPath: "itemtype")
tableView.reloadData()
default: break
}
}
你为什么这样做:
_ = allProducts?.sorted(byKeyPath: "basedescription")
您忽略了结果,因此没有任何变化。 sorted
方法不更新发件人,它 returns 一个新集合。
您需要更新 allProducts
,以便 table 视图在您重新加载时发生变化。
您可能想要(如果 Realm 支持):
allProducts?.sort(byKeyPath: "basedescription")
或:
allProducts = allProducts?.sorted(byKeyPath: "basedescription")
当然你也需要更新其他案例。
我正在使用 Realm 将数据加载到我的 UITableView 中,并且我已经将 UISegmentedControl 设置为导航中的标题;但是当 segmentedControlChanged 被触发时,我的 tableView 中没有任何变化。
var productViewSegmentedControl: UISegmentedControl? = nil
let realm = try! Realm()
var allProducts : Results<Product>?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if allProducts == nil {
allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
}
if productViewSegmentedControl == nil {
let segmentedControlItems = ["List", "Brands", "Categories"]
productViewSegmentedControl = UISegmentedControl(items: segmentedControlItems)
productViewSegmentedControl?.selectedSegmentIndex = 0
self.navigationItem.titleView = productViewSegmentedControl
productViewSegmentedControl?.addTarget(self, action: #selector(OrderFormViewController.segmentedControlChanged(_:)), for:.allEvents)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func segmentedControlChanged(_ segControl: UISegmentedControl){
switch segControl.selectedSegmentIndex{
case 0:
_ = allProducts?.sorted(byKeyPath: "basedescription")
tableView.reloadData()
case 1:
_ = allProducts?.sorted(byKeyPath: "itembrand")
tableView.reloadData()
case 2:
_ = allProducts?.sorted(byKeyPath: "itemtype")
tableView.reloadData()
default: break
}
}
你为什么这样做:
_ = allProducts?.sorted(byKeyPath: "basedescription")
您忽略了结果,因此没有任何变化。 sorted
方法不更新发件人,它 returns 一个新集合。
您需要更新 allProducts
,以便 table 视图在您重新加载时发生变化。
您可能想要(如果 Realm 支持):
allProducts?.sort(byKeyPath: "basedescription")
或:
allProducts = allProducts?.sorted(byKeyPath: "basedescription")
当然你也需要更新其他案例。