UISearchbar 和在视图控制器中传递数据 xcode swift
UISearchbar and passing data in View a controller xcode swift
所以我有一个 table 视图和一个搜索栏,我实现了搜索栏的代码来过滤我的数组它工作得很好但是当我点击我搜索的单元格时它不显示正确的信息它只显示第一行(第一个索引)的信息,请注意我正在使用全局数组将数据从我的 table 视图传递到视图控制器所以有人可以告诉我我的问题在哪里代码我该如何解决它。
class SciencesVC: UIViewController ,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var TableView: UITableView!
var data = ["د.غادة المطيري", "د.حياة سندي"]
var filteredData = [String]()
var inSearchMode = false
override func viewDidLoad() {
super.viewDidLoad()
TableView.delegate = self
TableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredData.count
}
return data.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 = data[indexPath.row]
}
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 = data.filter({[=12=].contains(searchBar.text!)})
TableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
myIndex = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
}
}
值是 SciencesVC 中的全局数组:
var Names = ["د.غادة المطيري ","د.حياة سندي "]
var majors = ["الكيمياء","عالمة صيدلية "]
var Communiction = ["-","-",""]
var myIndex = 0
这里是详细的UIViewController展示信息
class ViewController: UIViewController {
@IBOutlet weak var Name: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var major: UILabel!
@IBOutlet weak var info: UITextView!
@IBOutlet weak var twitter: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Name.text = Names[myIndex]
info.text = infos [myIndex]
myImageView.image = UIImage(named: Names[myIndex] + ".png")
twitter.text = Communiction[myIndex]
}
}
抱歉,我正在使用 iPhone,但试试看:
在 didSelectRowAt
中,您必须检查是否使用 searchBar 进行过滤。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if inSearchMode {
myIndex = data.index(of: filteredData[indexPath.row])
} else {
myIndex = indexPath.row
}
performSegue(withIdentifier: "segue", sender: self)
}
您可以使用 prepare(for segue: UIStoryboardSegue, sender: Any?)
方法将必要的索引发送到 detailViewController。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard let detailViewController = segue.destination as? UIViewController else { fatalError() }
guard let selectedCell = sender as? TableViewCell else { fatalError() }
guard let indexPath = tableView.indexPath(for: selectedCell) else { fatalError() }
if inSearchMode {
selectedIndex = data.index(of: filteredData[indexPath.row])!
} else {
selectedIndex = indexPath.row
}
detailViewController.myIndex = selectedIndex
}
所以我有一个 table 视图和一个搜索栏,我实现了搜索栏的代码来过滤我的数组它工作得很好但是当我点击我搜索的单元格时它不显示正确的信息它只显示第一行(第一个索引)的信息,请注意我正在使用全局数组将数据从我的 table 视图传递到视图控制器所以有人可以告诉我我的问题在哪里代码我该如何解决它。
class SciencesVC: UIViewController ,UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var TableView: UITableView!
var data = ["د.غادة المطيري", "د.حياة سندي"]
var filteredData = [String]()
var inSearchMode = false
override func viewDidLoad() {
super.viewDidLoad()
TableView.delegate = self
TableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredData.count
}
return data.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 = data[indexPath.row]
}
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 = data.filter({[=12=].contains(searchBar.text!)})
TableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
myIndex = indexPath.row
performSegue(withIdentifier: "segue", sender: self)
}
}
值是 SciencesVC 中的全局数组:
var Names = ["د.غادة المطيري ","د.حياة سندي "]
var majors = ["الكيمياء","عالمة صيدلية "]
var Communiction = ["-","-",""]
var myIndex = 0
这里是详细的UIViewController展示信息
class ViewController: UIViewController {
@IBOutlet weak var Name: UILabel!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var major: UILabel!
@IBOutlet weak var info: UITextView!
@IBOutlet weak var twitter: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Name.text = Names[myIndex]
info.text = infos [myIndex]
myImageView.image = UIImage(named: Names[myIndex] + ".png")
twitter.text = Communiction[myIndex]
}
}
抱歉,我正在使用 iPhone,但试试看:
在 didSelectRowAt
中,您必须检查是否使用 searchBar 进行过滤。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if inSearchMode {
myIndex = data.index(of: filteredData[indexPath.row])
} else {
myIndex = indexPath.row
}
performSegue(withIdentifier: "segue", sender: self)
}
您可以使用 prepare(for segue: UIStoryboardSegue, sender: Any?)
方法将必要的索引发送到 detailViewController。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard let detailViewController = segue.destination as? UIViewController else { fatalError() }
guard let selectedCell = sender as? TableViewCell else { fatalError() }
guard let indexPath = tableView.indexPath(for: selectedCell) else { fatalError() }
if inSearchMode {
selectedIndex = data.index(of: filteredData[indexPath.row])!
} else {
selectedIndex = indexPath.row
}
detailViewController.myIndex = selectedIndex
}