【发布时间】:2021-05-09 09:00:33
【问题描述】:
我有一个表格,每个单元格中都有一个名称和一张图片。我有一个搜索栏,可以搜索确实成功发生的名称,但是图像是空白的。当您从搜索栏中删除搜索时,单元格中的图像也会消失!有谁知道我做错了什么,如果可以,请有人帮助我!
谢谢
- 在删除搜索时出现问题,图像未显示,但现在由于 Raja 已得到修复
- 唯一剩下的问题是它在搜索时不过滤图像。搜索单元格时图像仍为空白
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({$0.prefix(searchText.count) == searchText } )
searching = true
if searchText.isEmpty {
searching = false
} else {
searching = true
}
tableView.reloadData()
}
}
【问题讨论】:
-
你必须添加一个额外的条件,比如``` if searchText.isEmpty { search = false } else { search code here } reload your table here```
-
您好 Raja,感谢您的意见。我已经实现了这个,它解决了当有人删除他们的搜索图像时显示的事实(这是以前的问题)所以我感谢你!
-
当时在搜索图像中删除时,还需要在主数组列表中删除该图像。
标签: swift uitableview uisearchbar