如何解决如果表格视图单元格是自定义数据类型,如何实现 UISearchBar
在我看过的所有 UISearchBar 实现教程中,表格视图单元格都来自一个简单的文本数组。我在实施搜索栏时遇到困难,出现以下情况:
还要注意,我的数据来自 Firestore。
这是我的视图控制器,用于显示我要过滤的表视图。
Game.stop()
这是我的自定义单元格:
import UIKit
class MealPlanViewController: UIViewController,UISearchBarDelegate {
private var model = MealPlanModel()
private var mealPlan = [MealPlan]()
private var isstarFiltered = false
var filteredData: [MealPlan]!
@IBOutlet weak var starButton: UIButton!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Menu"
navigationController?.navigationBar.prefersLargeTitles = true
// Set ViewController as the datasource and delegate of the tableView
tableView.delegate = self
tableView.dataSource = self
// Use MealPlanModel to get the mealPlans from the JSON File
model.delegate = self
searchBar.delegate = self
filteredData = mealPlan
// Set the status of the Star Button
setStarFilterButton()
// Retrieve all Meal Plans according to the filter status
if isstarFiltered {
model.getMealPlans(true)
}
else {
model.getMealPlans()
}
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
// Detect the indexPath the user selected
let indexPath = tableView.indexPathForSelectedRow
// Check that the indexPath isn't nil
guard indexPath != nil else {
// The user hasn't selected anything
return
}
// Get the mealPlan the user selected
let mealPlanSelected = mealPlan[indexPath!.row]
// Get a reference to the InstructionViewController
let ingredientsVC = segue.destination as! IngredientsViewController
// Get a reference to the currentMealPlanIndex in the InstructionViewController - https://www.hackingwithswift.com/example-code/system/how-to-pass-data-between-two-view-controllers
ingredientsVC.currentMealPlanIndex = indexPath!.row
}
func setStarFilterButton() {
let imageName = isstarFiltered ? "star.fill" : "star"
starButton.setimage(UIImage(systemName: imageName),for: .normal)
}
@IBAction func starButtonTapped(_ sender: Any) {
// Toggle the star filter status
isstarFiltered.toggle()
// Run the query
if isstarFiltered {
model.getMealPlans(true)
}
else {
model.getMealPlans()
}
// Update the starButton
setStarFilterButton()
}
func searchBar(_ searchBar: UISearchBar,textDidChange searchText: String) {
filteredData = []
if searchText == "" {
filteredData = mealPlan
}
else {
for item in mealPlan {
if ((item.title?.lowercased().contains(searchText.lowercased())) != nil) {
filteredData.append(item)
}
}
}
self.tableView.reloadData()
}
}
extension MealPlanViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return mealPlan.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Get a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell",for: indexPath) as! MealPlanCell
// Get the mealPlan that the tableView is asking about
let mealPlanInTable = mealPlan[indexPath.row]
// Customize the cell
cell.displayMealPlan(mealPlanInTable)
// Round the corners
cell.mealPlanView.layer.cornerRadius = 10
cell.coverImageView.layer.cornerRadius = 10
// Return the cell
return cell
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
}
}
extension MealPlanViewController: MealPlanProtocol {
func mealPlansRetrieved(mealPlans: [MealPlan]) {
print("MealPlan retrieved from model (MealPlanViewController).")
// Set the mealPlans property of the ViewController to the mealPlans passed back from the model
self.mealPlan = mealPlans
// Refresh the tableView
tableView.reloadData()
}
}
这是在我的 class MealPlanCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var recipeSourceLabel: UILabel!
@IBOutlet weak var estimatedTimeLabel: UILabel!
@IBOutlet weak var coverImageView: UIImageView!
@IBOutlet weak var mealPlanView: UIView!
var mealPlanTodisplay:MealPlan?
func displayMealPlan(_ mealPlan:MealPlan) {
// Keep a reference to the mealPlan
mealPlanTodisplay = mealPlan
// Set the titleLabel
titleLabel.text = mealPlanTodisplay!.title
// Set the numberOfRecipesLabel
recipeSourceLabel.text = mealPlanTodisplay!.recipeSource
// Set the estimatedTimeLabel
estimatedTimeLabel.text = mealPlanTodisplay!.estimatedTime
// Set the coverImageView
if mealPlanTodisplay!.coverImageView == nil {
return
}
coverImageView.image = UIImage(named: mealPlanTodisplay!.coverImageView!)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
// Configure the view for the selected state
}
}
函数中返回的结构:
displayMealPlan
项目正在构建,没有错误,但当我在搜索栏中输入时没有任何反应。我认为这可能是几件事,但我不确定:
- 我必须更改
struct MealPlan { var docID:String? var title:String? var recipeSource:String? var estimatedTime:String? var coverImageView:String? var isstarred:Bool? }
和numberOfRowsInSection
方法中的返回值才能返回过滤后的数据 - 按
cellForRowAt
过滤是不正确的方法
非常感谢任何帮助或指导!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。