swift - 当我搜索搜索 Controller 时,indexPath 不会随原始数组改变

标签 swift uitableview search

我确实在我的应用中实现了搜索页面。我将 PFObjects 放在 postsArray 中,当用户搜索某些内容时,搜索到的对象进入 filteredArray。这就是我试图做的。

tableView 显示结果,但是当我点击它时,它显示 postArray indexPath,这是我搜索之前的 indexPath。例如,original(postsArray) 第一项是红色的,我搜索 yello,当我点击黄色的 tableView Cell 时,它显示红色的帖子。

这是我的代码。

override func viewDidLoad() {
super.viewDidLoad()


self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self

self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()

self.myTable.tableHeaderView = self.resultSearchController.searchBar
self.myTable.reloadData()

self.bringAllDatafromParse()

}


  func updateSearchResultsForSearchController(searchController: UISearchController) {

  self.filterdArray.removeAllObjects()

   let normalizedSearchText = 

searchController.searchBar.text!.lowercaseString

 for posts in self.postsArray {
    var title = ""
    var tag = ""
    if let titleText = posts["titleText"] as? String {
        title = titleText
    }
    if let tagText = posts["tagText"] as? String {
        tag = tagText
    }
    let results = "\(title) \(tag)"

    if results.lowercaseString.rangeOfString(normalizedSearchText) != nil {
        self.filterdArray.addObject(posts)
    }

}

self.myTable.reloadData()

}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if self.resultSearchController.active{
    return self.filterdArray.count
}else
{
    return self.postsArray.count
}

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! SearchTVCE
//cell.textLabel!.text = searchResults[indexPath.row]



var postObjects : PFObject!


if self.resultSearchController.active{
     postObjects = self.filterdArray.objectAtIndex(indexPath.row) as! PFObject
}else {

      postObjects = self.postsArray.objectAtIndex(indexPath.row) as! PFObject


}
//솔드
cell.soldLabel.hidden = true

if (postObjects.objectForKey("sold") as! Bool) == true {
    cell.soldLabel.hidden = false

}


// 제목
cell.titleLabel.text = (postObjects.objectForKey("titleText") as! String)

+ " : " + (postObjects.objectForKey("tagText") as! String)


 return cell

}

我的问题是如何在从搜索栏获得结果后获得正确的 indexPath。

在我搜索并点击它之后,它会出现另一个 View 。但搜索栏不会消失,除非我点击取消栏按钮。我该如何解决这个问题?

最佳答案

您的代码看起来不错。

但是,您没有显示可能为您的问题提供线索的 didSelectRowAtIndexPath 函数。

我的猜测是——你得到的索引路径是正确的,因为你总是在 tableview 上链接 self.filterdArray。据我了解,UI 上的所有内容看起来都不错,但只有当您点击第一个单元格时,新的 View Controller 才会加载属于非过滤数组第一个单元格的数据。我建议您检查一下您的 didSelectRowAtIndexPath 并查看是否正确获取了数据。它应该看起来像这样:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if (self.resultSearchController.active) {
        myData = (self.filterdArray[indexPath.row])!
    } else {
        myData = (self.postsArray[indexPath.row])!
    }

    self.performSegueWithIdentifier("MySegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "MySegue") {
        let myViewController = segue.destinationViewController as MyViewController
        myViewController.data = self.myData
    }
}

还有你的另一个问题:

searchbar doesn't disappear unless I tap on cancel bar button. How do I fix this?

只需调用:searchController.active = false

关于swift - 当我搜索搜索 Controller 时,indexPath 不会随原始数组改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314123/

相关文章:

ios - 如何在 App Store Connect 中添加本地化?

swift - 分享按钮在 iPhone 上完美运行,但在 iPad 上崩溃

ios - 动画时将 UIImageView 置于最前面

search - Elasticsearch 返回给定类型的所有文档

Python 加速在范围字典中搜索值

swift - 如何在 Xcode Swift 4 中复制一个类以使其独立于其父类

swift - UITableViewCell : rounded corners and shadow

ios - 对 UITableView 部分使用一个 NSManagedObject,它与另一个 NSManagedObject 的行有关系吗?

ios - swift 2.0,UITableView : cellForRowAtIndexPath returning nil for non-visible cells

python - 遍历一组字典 - python - 基本搜索