在我的视图集合中,我添加了一个搜索栏,它过滤了单元格,但是当我单击单元格时,它自己的路径没有改变
Posted
技术标签:
【中文标题】在我的视图集合中,我添加了一个搜索栏,它过滤了单元格,但是当我单击单元格时,它自己的路径没有改变【英文标题】:In my view collection I added a search bar and it filtered the cells but when I click the cell it it self it the path didn't change 【发布时间】:2021-12-04 19:09:51 【问题描述】:在我的视图集合中,我添加了一个搜索栏,它过滤了单元格,但是当我单击它自己的单元格时,路径没有改变,路径保持与搜索前相同。
当我单击单元格时,它应该带您到特定页面。请原谅我糟糕的代码,但我刚刚开始学习 swift。我的问题可能非常简单和明显,所以请对我说实话
这是我正在使用 swift storyboard 的代码
import UIKit
import Firebase
import FirebaseStorage
class resViewViewController: UIViewController, UISearchBarDelegate, UISearchDisplayDelegate
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var resL: UILabel!
@IBOutlet weak var collection: UICollectionView!
var resources:[resFile] = []
let db = Firestore.firestore()
//dummy data
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
var filtered:[resFile] = []
override func viewDidLoad()
super.viewDidLoad()
let nipCell = UINib(nibName: "resourceCellCollectionViewCell", bundle: nil)
//
collection.delegate = self
collection.dataSource = self
searchBar.delegate = self
///
collection.register(nipCell, forCellWithReuseIdentifier: "cell")
loadResources()
func loadResources()
db.collection("Resources").getDocuments querySnapshot, error in
if let e = error
print("There was an issue retrieving data from fireStore. \(e)")
else
if let snapshotDocuments = querySnapshot?.documents
for doc in snapshotDocuments
let data = doc.data()
if let rName = data["ResName"] as? String, let aName = data["authorName"] as? String, let pName = data["pubName"] as? String, let desc = data["desc"] as? String, let urlName = data["url"] as? String
let newRes = resFile(name: rName, author: aName, publisher: pName, desc: desc, urlString: urlName)
self.resources.append(newRes)
DispatchQueue.main.async
self.collection.reloadData()
// DispatchQueue.main.async
// self.collection.reloadData()
//
//end loadResources
//search
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
searchActive = true;
func searchBarTextDidEndEditing(_ searchBar: UISearchBar)
searchActive = false;
self.searchBar.endEditing(true)
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
searchActive = false;
self.searchBar.endEditing(true)
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
searchActive = false;
self.searchBar.endEditing(true)
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
filtered = resources.filter $0.name.localizedCaseInsensitiveContains(searchText)
if(filtered.count == 0)
searchActive = false;
else
searchActive = true;
self.collection.reloadData()
//end of class
extension resViewViewController:UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
let w = (UIScreen.main.bounds.size.width - 110)/2
return CGSize(width: w, height: 160) //154
//end size
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if(searchActive)
return filtered.count
else
return resources.count
//end count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collection.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! resourceCellCollectionViewCell
if(searchActive)
cell.name.text = filtered[indexPath.row].name
else
cell.name.text = resources[indexPath.row].name
return cell
//end cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
self.performSegue(withIdentifier: "si_resourceListToDetail", sender: indexPath)
//end
//extention
extension resViewViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "si_viewResToPost", let vc = segue.destination as? resPostViewController
vc.delegate = self
else if segue.identifier == "si_resourceListToDetail",
let vc = segue.destination as? detailedResViewController, let indexPath = sender as? IndexPath
vc.resource = resources[indexPath.row]
//extension
extension resViewViewController: resPostViewControllerDelegate
func resPost(_ vc: resPostViewController, resource: resFile?, added: Bool)
vc.dismiss(animated: true)
if added, let r = resource
self.resources.append(r)
self.collection.reloadData()
//extension
【问题讨论】:
【参考方案1】:如果您选择 UICollectionView 中的第一个可见单元格,则 indexPath 将始终相同。您必须检查基础数据集才能获得差异。
另外:在 Swift 中,类和结构的首字母大写是惯例。
你必须这样做才能实现你想要的:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let item: ResFile
if searchActive
item = filtered[indexPath.item]
else
item = resources[indexPath.item]
performSegue(withIdentifier: "si_resourceListToDetail", sender: item)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
guard let item = sender as? ResFile else return
// Send item to destination view controller
【讨论】:
我刚刚更新了我的代码,你能再检查一遍吗,因为我已经有了准备功能 好吧,没关系,我修复了它,但感谢您让我知道 indexPath 它有很大帮助(:以上是关于在我的视图集合中,我添加了一个搜索栏,它过滤了单元格,但是当我单击单元格时,它自己的路径没有改变的主要内容,如果未能解决你的问题,请参考以下文章