[英]Swift Multi Selection TableView occurs error while unselecting rows
[英]SWIFT tableView rows do not react to selection
一些上下文:
我正在用電影標題填充tableView
當這些行之一被選中時,我想轉到電影詳細信息,但是當我點擊任何行時,都不會發生。
class SearchTableViewController: NavigationController{
@IBOutlet weak var tableView: UITableView!
var filteredMovies = [Movie]()
let request = Requests()
override func viewDidLoad() {
super.viewDidLoad()
}
/*
searches movies from the db which title corresponds to the given text
*/
func searchMovie(_ keywords: String) {
request.searchMovie(keywords: keywords){ response in
for data in response{
self.filteredMovies.append(data)
}
DispatchQueue.main.sync {
self.tableView?.reloadData()
}
}
}
}
extension SearchTableViewController : UITableViewDelegate { }
extension SearchTableViewController: UISearchBarDelegate {
/*
every time a key gets pressed, the table view gets updateted
with new movie titles
*/
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.filteredMovies = [Movie]()
searchText.isEmpty ? self.tableView?.reloadData() : searchMovie(searchText)
}
}
extension SearchTableViewController : UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredMovies.count
}
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
print("hello from highlight")
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("hello from selection")
}
func tableView(_ tableView: UITableView, didFocusRowAt indexPath: IndexPath) {
print("hello from focus")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SearchTableCell
cell.onDidSelectMovie = {
super.goToMovieDetail(movie: self.filteredMovies[indexPath.row])
}
let label = cell.contentView.subviews.first as! UILabel
label.text = filteredMovies[indexPath.row].title
return cell
}
}
如您所見,我測試了可能顯示出行被點擊但沒有一個起作用的每個函數。 我還嘗試為單元格提供一個自定義類,在該類中我重寫單元格屬性“ isSelected”的行為。
class SearchTableCell: UITableViewCell {
var onDidSelectMovie:(()->Void)?
override var isSelected: Bool{
didSet {
if isSelected{
if let cb = onDidSelectMovie{
cb()
}
}
}
}
}
我想指出的另一件事可能會有所幫助,那就是像“用戶交互”一樣啟用了單元格上的“選擇”,但是如果我嘗試將選擇從“默認”更改為“藍色”,則顏色不會改變。
我從字面上耗盡了想法,嘗試了許多可能的解決方案,但沒有一個起作用。 有什么建議嗎?
編輯:我將添加所有可能有用的內容
看來我只需要刪除並再次添加tableView委托...不要問我為什么,我已經花了2天的時間。 無論如何,感謝所有嘗試提供幫助的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.