过滤后的 didSelectRowAtIndexPath 索引路径 UISearchController - Swift [英] didSelectRowAtIndexPath indexpath after filter UISearchController - Swift

查看:24
本文介绍了过滤后的 didSelectRowAtIndexPath 索引路径 UISearchController - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个分区表中实现一个带有 UISearchController 的搜索栏.到目前为止一切顺利.

I’m implementing a search bar with an UISearchController within a sectioned table. So far so good.

主要问题是当过滤结果出现时,它是一个全新的表格,没有节和更少的行.

The main issue is that when the the filtered results come along, it’s a whole new table with no sections and fewer rows.

选择行时,我对数组中的那个位置执行了segue,但详细视图期望主数组中的确切行或索引,我无法从过滤的对象数组中获得,这可能在 300 个元素中为 [0] [1] [2].

When selecting the row, I perform a segue to that position in the array, but the detailed view is expecting that exact row or index from the main array, which I can’t get from the filtered array of objects, which may be [0] [1] [2] in 300 elements.

我想我可以将选定的对象与主数组进行比较,并假设没有重复项,从那里获取索引并将其传递……但这些对我来说似乎效率很低.

I guess I can compare the selected object with the main array and assuming there’s no duplicates, get the index from there and pass it over… But these seems pretty inefficient to me.

Apple 在联系人应用程序中过滤联系人时做了类似的事情(不幸的是我不知道如何).他们如何传递接触对象?这几乎就是我的目标.

Apple does something similar (I unfortunately don’t know how) when filtering Contacts, in the Contacts App. How they pass the contact object? That’s pretty much my goal.

在此,我向您展示我正在做的事情的片段:

Here I let you a snippet of what I’m doing:

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

        if(self.resultSearchController.active) {
            customerAtIndex = indexPath.row // Issue here
            performSegueWithIdentifier("showCustomer", sender: nil)
        }
        else {
            customerAtIndex = returnPositionForThisIndexPath(indexPath, insideThisTable: tableView)
            performSegueWithIdentifier("showCustomer", sender: nil)
        }
    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showCustomer" {
            if let destination = segue.destinationViewController as? CustomerDetailViewController {
                destination.newCustomer = false
                destination.customer = self.customerList[customerAtIndex!]
                destination.customerAtIndex = self.customerAtIndex!
                destination.customerList = self.customerList
            }
        }
    }

推荐答案

你可以用另一种方式来做,这是一个技巧,但它有效.首先更改您的 didSelectRowAtIndexPath 如下:

You can either do in another way, it a trick, but it works. First change your didSelectRowAtIndexPath as below:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var object :AnyObject?
        if(self.resultSearchController.active) {
            object = filteredArray[indexPath.row]
        }
        else {
            object = self.customerList[indexPath.row]
        }

        performSegueWithIdentifier("showCustomer", sender: object)
    }

现在,在 prepareForSegue 中,取回对象并将其发送到您的详细视图控制器

Now, in prepareForSegue, get back the object and send it to your detailed view controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showCustomer" {
        if let destination = segue.destinationViewController as? CustomerDetailViewController {
            destination.newCustomer = false
            destination.customer = sender as! CustomerObject
            destination.customerAtIndex = self.customerList.indexOfObject(destination.customer)
            destination.customerList = self.customerList
        }
    }
}

这篇关于过滤后的 didSelectRowAtIndexPath 索引路径 UISearchController - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆