我正在分区表中使用 UISearchController 实现搜索栏。到目前为止一切顺利。
主要问题是,当过滤结果出现时,它是一个全新的表格,没有部分且行数较少。
选择行时,我在数组中执行一个位置,但是详细的 View 期望从主数组中获得精确的行或索引,而我无法从过滤的对象数组中获得,这可能在 300 个元素中为 [0] [1] [2]。
我想我可以将所选对象与主数组进行比较,并假设没有重复项,从那里获取索引并将其传递过来……但这些对我来说似乎效率很低。
Apple 在联系人应用程序中过滤联系人时做了类似的事情(不幸的是我不知道怎么做)。他们如何传递接触对象?这几乎就是我的目标。
在这里,我向您展示了我正在做的事情的片段:
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 如下:
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 中,取回对象并将其发送到详细 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
}
}
}
关于ios - 过滤器 UISearchController 之后的 didSelectRowAtIndexPath indexpath - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29855457/