无法使用搜索栏中的图像进行搜索 Table 查看单元格:Swift

Unable To Search with Image In Search Bar Table View Cell: Swift

我有一个 table,每个单元格中都有一个名称和一张图片。我有一个搜索栏,可以搜索成功发生的名称,但图像是空白的。当您从搜索栏中删除搜索时,单元格中的图像也会消失!有谁知道我做错了什么吗?如果可以,请有人帮助我!

谢谢

import UIKit

class TestTableViewController: UITableViewController {
    
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var userWorkoutName: UILabel!

    var valueToPass: String!

    var workoutName = ["Apple","Orange","Banana"]
    
    var workoutImage = ["A","O","B"] 
    
    var searchingWorkouts = [String()]
    
    var searching = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.delegate = self
        searchingWorkouts = workoutName
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if searching {
           return searchingWorkouts.count
        } else {
            return workoutName.count
        }
    
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIentifier, for: indexPath) as! WorkoutTableViewCell
                        
        if searching {
            
            cell.workoutName.text =  searchingWorkouts[indexPath.row]
            cell.workoutImage.image = UIImage(named: searchingWorkouts[indexPath.row])
            
        } else {
            
            cell.workoutName.text =  workoutName[indexPath.row]
            cell.workoutImage.image = UIImage(named: workoutImage[indexPath.row])
            
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let moreDetail = storyboard.instantiateViewController(identifier: "UploadWorkoutViewController") as! UploadWorkoutViewController
        
       if searching {
            
            moreDetail.getWorkoutTitle = searchingWorkouts[indexPath.row]
            
     } else {
            
            moreDetail.getWorkoutTitle = workoutName[indexPath.row] 
            
        }
        
        self.navigationController?.pushViewController(moreDetail, animated: true)
        
    }  
}

extension TestTableViewController: UISearchBarDelegate {
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        
        searchingWorkouts = workoutName.filter({[=12=].prefix(searchText.count) == searchText } )
        
        searching = true

    if searchText.isEmpty {
            searching = false
        } else {
            searching = true
        }
        
        tableView.reloadData()     
    }
} 

图像是空白的,因为您没有过滤图像,您只过滤了锻炼名称。在搜索时,您将 searchingWorkouts 分配给图像,这是完全错误的。

cell.workoutImage.image = UIImage(named: searchingWorkouts[indexPath.row])

就像维护searchingWorkouts一样,您也需要维护searchingWorkoutImage。然后把上面一行改成这个

cell.workoutImage.image = UIImage(named: searchingWorkoutImage[indexPath.row])

但问题是您将如何过滤图像名称?因为锻炼名称和图像名称不同。

所以更好的解决方案是创建一个 Workout class 具有 nameimage 属性并将代码更改为以下

class Workout {
    var name: String = ""
    var image: String = ""
    
    init(name: String, image: String) {
        self.name = name
        self.image = image
    }
}

class TestTableViewController: UITableViewController {
    
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var userWorkoutName: UILabel!

    var valueToPass: String!
    
    var workouts = [Workout(name: "Apple", image: "A"), Workout(name: "Orange", image: "O")]
    var searchingWorkouts = [Workout]()
    
    var searching = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.delegate = self
        searchingWorkouts = workouts
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if searching {
           return searchingWorkouts.count
        } else {
            return workouts.count
        }
    
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIentifier, for: indexPath) as! WorkoutTableViewCell
                        
        if searching {
            
            cell.workoutName.text =  searchingWorkouts[indexPath.row].name
            cell.workoutImage.image = UIImage(named: searchingWorkouts[indexPath.row].image)
            
        } else {
            
            cell.workoutName.text =  workouts[indexPath.row].name
            cell.workoutImage.image = UIImage(named: workouts[indexPath.row].image)
            
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let moreDetail = storyboard.instantiateViewController(identifier: "UploadWorkoutViewController") as! UploadWorkoutViewController

       if searching {

        moreDetail.getWorkoutTitle = searchingWorkouts[indexPath.row].name

     } else {

        moreDetail.getWorkoutTitle = workouts[indexPath.row].name

        }

        self.navigationController?.pushViewController(moreDetail, animated: true)
        
    }
}

extension TestTableViewController: UISearchBarDelegate {
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        
        searchingWorkouts = workouts.filter({[=12=].name.prefix(searchText.count) == searchText } )
        
        searching = true

    if searchText.isEmpty {
            searching = false
        } else {
            searching = true
        }
        
        tableView.reloadData()
    }
}