swift - 为什么过滤器数据不显示在表格视图中?
问题描述
所以我有以下代码,我通过单击 2 个按钮过滤马是否接种疫苗。当我调试时,我看到我创建的应存储已过滤数据的附加数组实际上已被过滤,但结果未显示在表格视图上,但未过滤数组中的旧数据仍然存在,为什么会这样以及如何我修复它 ?
这是我的代码:
var horses = [Horse] ()
var filteredData = [Horse]()
@IBAction func btnNotVaccined(_ sender: Any) {
self.filteredData = horses.filter { !$0.vaccinated } // or !$0.vaccinated for the second option
self.tableView.reloadData()
print(horses.count)
print(filteredData.count)
print("Not Vaccined")
}
@IBAction func btnVaccined(_ sender: Any) {
filteredData = horses.filter { $0.vaccinated } // or !$0.vaccinated for the second option
self.tableView.reloadData()
print(horses.count)
print(filteredData.count)
print(" Vaccined")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.horses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "horseCell", for: indexPath)
// let buttonCell = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath)
cell.textLabel?.text = "Name: \(self.horses[indexPath.row].name) "
//Name: \(self.horses[indexPath.row].name), \(self.horses[indexPath.row].name)"
cell.detailTextLabel?.text = String(self.horses[indexPath.row].medicalReports[0].description )
cell.imageView?.image = UIImage(named: "horse")
return cell
// tableView.deselectRow(at: indexPath, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let numbers = 0...1
let medRep = MedicalReport(id: 1,description: "Flu: Vaccinated, Decontamination: Vaccinated",dateOfTreatment: Date())
var medicalReports = [MedicalReport] ()
medicalReports.append(medRep)
for _ in numbers{
let horse = Horse(id: 1,name: "Angel",race: "White",gender: Gender.MALE ,medicalReports: medicalReports, vaccinated : false)
let horse2 = Horse(id: 1,name: "Rambo",race: "White",gender: Gender.MALE ,medicalReports: medicalReports, vaccinated : true)
// print(medRep.description)
horses.append(horse);
horses.append(horse2);
self.tableView.reloadData()
// Do any additional setup after loading the view.
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
解决方案
尝试这些代码更改。这是基于您的原始代码。它创造了四匹马:两匹叫做天使,两匹叫做兰博——这就是你想要的吗?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "horseCell", for: indexPath)
cell.textLabel?.text = "Name: \(self.filteredData[indexPath.row].name) "
cell.detailTextLabel?.text = String(self.filteredData[indexPath.row].medicalReports[0].description )
cell.imageView?.image = UIImage(named: "horse")
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let numbers = 0...1
let medRep = MedicalReport(id: 1, description: "Flu: Vaccinated, Decontamination: Vaccinated",dateOfTreatment: Date())
var medicalReports = [MedicalReport] ()
medicalReports.append(medRep)
for _ in numbers {
let horse = Horse(id: 1, name: "Angel",race: "White",gender: Gender.MALE ,medicalReports: medicalReports, vaccinated : false)
let horse2 = Horse(id: 1, name: "Rambo",race: "White",gender: Gender.MALE ,medicalReports: medicalReports, vaccinated : true)
horses.append(horse);
horses.append(horse2);
}
filteredData = horses
self.tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
推荐阅读
- php - 在 7.0 ubuntu 16.04 旁边安装 php 5.6 后,本地虚拟主机停止工作
- javascript - Array.prototype.find() 在异步函数中返回未定义
- python - 从python创建可执行文件时出错
- php - 试图从日期对象中获取年/月/日,但它返回 1970-01-01
- ios - 如何在 MKPointAnnotation 上添加 Tap 手势以显示另一个数据视图?
- c - Linux C中的同步问题同时读取和写入文件
- f# - F# - 获取模块中的方法列表
- angularjs - 如何从angularjs中的另一个控制器获取ng-model值
- c# - ASP.Net 面板到图像
- angular - 不了解将 Oauth 集成到 REST API 和单页应用程序的工作流程