![](/img/trans.png)
[英]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.