Ios 关于SearchBar和Tableview的混淆
没有搜索,当我点击任何单元格时,警报显示正确的姓氏。然而,不知何故,当我搜索一个名字并点击它的单元格时,警报显示了错误的姓氏。在任何情况下(我的意思是,我搜索任何东西),警报总是显示Quuen姓氏。我想,因为当我搜索时,结果总是成为我的tableview的第一个索引 我的代码:Ios 关于SearchBar和Tableview的混淆,ios,uitableview,swift4,Ios,Uitableview,Swift4,没有搜索,当我点击任何单元格时,警报显示正确的姓氏。然而,不知何故,当我搜索一个名字并点击它的单元格时,警报显示了错误的姓氏。在任何情况下(我的意思是,我搜索任何东西),警报总是显示Quuen姓氏。我想,因为当我搜索时,结果总是成为我的tableview的第一个索引 我的代码: import UIKit struct Human { var name = String() var surname = String() } class ViewController: UIVi
import UIKit
struct Human {
var name = String()
var surname = String()
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var members = [Human]()
var filteredData = [String]()
var inSearchMode = false
var names = ["Oliver","Harold","John","Thea","Felicity"]
var surnames = ["Queen","Finch","Reese","Queen","Smoak"]
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<names.count {
members.append(Human(name: names[i],surname: surnames[i]))
}
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
}
// MARK: - UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredData.count
}
return members.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCell {
let text: String!
if inSearchMode {
text = filteredData[indexPath.row]
} else {
text = members[indexPath.row].name
}
cell.congigureCell(text: text)
return cell
} else {
return UITableViewCell()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
view.endEditing(true)
tableView.reloadData()
} else {
inSearchMode = true
filteredData = names.filter { $0.lowercased().contains(searchBar.text!.lowercased()) }
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showAlert(title: "Surname is...", msg: members[indexPath.row].surname)
}
}
extension UIViewController {
func showAlert(title: String, msg: String) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
导入UIKit
结构人{
var name=String()
变量姓氏=字符串()
}
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、UISearchBarDelegate{
@IBVAR表格视图:UITableView!
@ibvar搜索栏:UISearchBar!
变量成员=[Human]()
变量filteredData=[String]()
var inSearchMode=false
变量名称=[“奥利弗”、“哈罗德”、“约翰”、“西娅”、“费利西蒂”]
var姓氏=[“女王”、“芬奇”、“里斯”、“女王”、“斯莫克”]
重写func viewDidLoad(){
super.viewDidLoad()
对于0..Int中的i{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果插入搜索模式{
返回filteredData.count
}
返回成员数
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
如果let cell=tableView.dequeueReusableCell(标识符为“cell”,for:indexPath)为?DataCell{
让文本:字符串!
如果插入搜索模式{
text=filteredData[indexPath.row]
}否则{
text=成员[indexath.row].name
}
cell.congigureCell(文本:text)
返回单元
}否则{
返回UITableViewCell()
}
}
func searchBar(searchBar:UISearchBar,textDidChange searchText:String){
如果searchBar.text==nil | | searchBar.text==“”{
inSearchMode=false
view.endEditing(真)
tableView.reloadData()
}否则{
inSearchMode=true
filteredData=names.filter{$0.lowercased().contains(searchBar.text!.lowercased())}
tableView.reloadData()
}
}
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
showAlert(标题:“姓氏是…”,消息:成员[indexPath.row]。姓氏)
}
}
扩展UIViewController{
func showAlert(标题:字符串,消息:字符串){
let alert=UIAlertController(标题:title,消息:msg,preferredStyle:.alert)
let action=UIAlertAction(标题:“确定”,样式:。默认,处理程序:nil)
alert.addAction(操作)
self.present(警报、动画:true、完成:nil)
}
}
屏幕截图如下:
最后,我如何解决这个问题?使用如下方法:
将filteredData
类型更改为Human
var filteredData = [Human]()
更改了cellForRow
,didSelectRowAt
和textDidChange
。
为更改的行添加了注释
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DataCell {
let text: String!
if inSearchMode {
text = filteredData[indexPath.row].name // changed this line only
} else {
text = members[indexPath.row].name
}
cell.textLabel?.text = text
return cell
} else {
return UITableViewCell()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
view.endEditing(true)
tableView.reloadData()
} else {
inSearchMode = true
filteredData = members.filter { $0.name.lowercased().contains(searchBar.text!.lowercased()) } // changed this line
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if inSearchMode {
showAlert(title: "Surname is...", msg: filteredData[indexPath.row].surname) // added this line
} else {
showAlert(title: "Surname is...", msg: members[indexPath.row].surname)
}
}
您发布的代码无法用于分析。请尝试发布更多代码,如搜索后如何过滤结果等,我认为这与您的问题无关,但“Queen”在您的姓氏数组中出现两次。