大家好,我已经尝试了几天没有找到答案。在我提出UITableViewDelegate
和UITableViewDataSource
这个问题之前,我已经实施了didSelectRowAt
和didDeselectRowAt
,这两种方法都无效。
class SearchClass: UIViewController, UITableViewDataSource,UITableViewDelegate, UISearchBarDelegate {
@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var mySearchBar: UISearchBar!
var objects:PFObject!
var searchResults = [String]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.navigationBar.topItem?.title = "Search"
self.navigationController?.navigationBar.barTintColor = UIColor.white
self.navigationController?.navigationBar.backgroundColor = UIColor.black
self.navigationController?.navigationBar.tintColor = UIColor.black
self.searchResults.removeAll()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myTableView.dataSource = self
self.mySearchBar.delegate = self
self.myTableView.delegate = self
self.navigationController?.extendedLayoutIncludesOpaqueBars = true
//self.myTableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
searchBar.resignFirstResponder()
// self.navigationController?.navigationBar.isHidden = false
self.mySearchBar.endEditing(true)
print("Search word = \(searchBar.text)")
let query = PFQuery(className:"myClass")
//let newText = searchBar.autocapitalization
searchBar.autocapitalizationType = .none
searchBar.text = searchBar.text?.localizedLowercase
query.whereKey("collegeNickName", contains: searchBar.text)
query.findObjectsInBackground { (results, error) in
if error == nil {
if let objects = results {
self.searchResults.removeAll(keepingCapacity: true)
for object in objects {
let firstName = object.object(forKey: "myName") as! String
let image = object.object(forKey: "myImage") as! PFFile
// let lastName = object.object(forKey: "myPlace") as! String
// let fullName = firstName + " " + lastName
self.searchResults.append(firstName)
print(self.searchResults[0])
DispatchQueue.main.async {
self.myTableView.reloadData()
self.mySearchBar.resignFirstResponder()
}
}
}
} else {
let myAlert = UIAlertController(title:"Alert", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil)
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
return
}
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
//self.navigationController?.navigationBar.isHidden = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell")
let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell")
myCell?.textLabel?.text = searchResults[indexPath.row]
print(searchResults[indexPath.row])
return myCell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hi")
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
mySearchBar.resignFirstResponder()
mySearchBar.text = ""
myTableView.reloadData()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
//self.mySearchBar.resignFirstResponder()
//self.mySearchBar.endEditing(true)
self.definesPresentationContext = true
}
@IBAction func refreshButtonTapped(sender: AnyObject) {
mySearchBar.resignFirstResponder()
mySearchBar.text = ""
self.searchResults.removeAll(keepingCapacity: false)
self.myTableView.reloadData()
}
}
它还实现了一个searchView我得到了我想要搜索的内容,但无法在我的课程中使用select和deselect方法。
答案 0 :(得分:1)
self.myTableView.dataSource = self
self.myTableView.delegate = self
< ---加上这个。
答案 1 :(得分:0)
注意到您已将故事板中的委托设置为UIViewController
而不是<source srcset="http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU6FOW3O0jR-t4LlattRw52wBmvg/ 240w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU6cKxp_v-TRYywK8kMonNsWFMiA/ 320w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU76IwFM89MgsU2CsVpABa94yrwg/ 500w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU7Lx-mjq8_Dk9iI7H4kcoPo-SmA/ 640w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU4kJMUl3WmTvlAFqfo4wIlDssvw/ 800w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU6-HA9n31rVvmbG5touqPt59wY3s/ 1024w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU6-dIS7lLTB0CSOM4O0wlvLx9pDnb/ 1600w, http://yourshot.nationalgeographic.com/u/fQYSUbVfts-T7odkrFJckdiFeHvab0GWOfzhj7tYdC0uglagsDq-TNIRQ3qELJppd8ZLNRvnhakVub3VQlC2V5_yAGtyNoIAtaUObf5sBn_PGVEIlVVcerfj6l1ovYy2W4h7lMAkEVLdiCZKr9S9wuwge1myLnbvmEvxjeQ-HOfdmgprhGjqn4pNtAwmKvwU6FcgiBNz-Nj7_J7e61F6_8oUXwoV/ 2048w" sizes="730px" data-reactid=".5.0.1.0.0.$http=2//www=1nationalgeographic=1com/photography/photo-of-the-day/2017/01/boy-buffalo-thailand.0.0.0.0.0.0.0.0">
see Discover - that's mine UViewController
答案 2 :(得分:0)
在Interface Builder中
delegate
的{{1}} 和 My Table View
连接到SearchClass
然后,您可以删除self.myTableView.dataSource = self
SearchClass
考虑Interface Builder中的连接比代码中的连接更有效。
答案 3 :(得分:0)
细胞宣言是错误的。你已经完成了以下代码
let myCell = myTableView.dequeueReusableCell(withIdentifier: "myCell")
正确的格式
let myCell = tableView.dequeueReusableCell(withIdentifier: "myCell")
原因:当您使用dequeue属性时,UITableView
将其单元格从方法中获取的参数出列
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
看到有一个参数tableView
,但您正在UITableView
的outlelet中出列。