首页 > 解决方案 > 为什么过滤器数据不显示在表格视图中?

问题描述

所以我有以下代码,我通过单击 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
       }

标签: swift

解决方案


尝试这些代码更改。这是基于您的原始代码。它创造了四匹马:两匹叫做天使,两匹叫做兰博——这就是你想要的吗?

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
}

推荐阅读