searchDisplayController,UITableView,Core Data和Swift

尝试使用searchdisplayController在表中搜索.配置数据过滤,搜索对话框有效.现在我想使用方法prepareForSegue将当前值indexPath发送到新的UIViewController:
import UIKit
import CoreData

class MainTableViewController: UITableViewController {

var results:AddrBook[]=[]
var searchResults:AddrBook[]=[]

init(style: UITableViewStyle) {
    super.init(style: style)
}

init(coder aDecoder: NSCoder!) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidAppear(animated: Bool) {
    let request = NSFetchRequest(entityName: "Person")
    request.returnsObjectsAsFaults = false
    let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    let context:NSManagedobjectContext = appDelegate.managedobjectContext
    results  = context.executeFetchRequest(request,error: nil) as AddrBook[]
    self.tableView.reloadData()
}

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    return 1
}

override func tableView(tableView: UITableView!,numberOfRowsInSection section: Int) -> Int {
    if tableView == self.searchdisplayController.searchResultsTableView {
        return searchResults.count
    } else {
        return results.count
    }
}

override func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell

    if !cell {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: "Cell")
    }

    if tableView == self.searchdisplayController.searchResultsTableView {
        cell!.textLabel.text = searchResults[indexPath.row].lastname + " " + searchResults[indexPath.row].firstname
        cell!.detailTextLabel.text = searchResults[indexPath.row].phonenumber
    } else {
        cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname
        cell!.detailTextLabel.text = results[indexPath.row].phonenumber
    }



    return cell
}

override func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    if tableView == self.searchdisplayController.searchResultsTableView {
        self.performSegueWithIdentifier("editPerson",sender : self)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject?) {

    var indexPath = NSIndexPath()
    if self.tableView == self.searchdisplayController.searchResultsTableView {
        NSLog("Trying recieve indexPath from Search")
        indexPath = self.searchdisplayController.searchResultsTableView.indexPathForSelectedRow()
        NSLog("indexPath from Search")
    }
    else {
        indexPath = self.tableView.indexPathForSelectedRow()
        NSLog("IndexPath from main table")
    }

    let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController

    if segue.identifier == "editPerson" {
        destViewController.receivedPerson = results
        destViewController.indexPath = indexPath
        NSLog("Selected person ID: \(results[indexPath.row].idperson)")
    }
}

override func tableView(tableView: UITableView?,canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool {
    return true
}


override func tableView(tableView: UITableView!,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath!) {

    let request = NSFetchRequest(entityName: "Person")
    request.returnsObjectsAsFaults = false
    let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
    let context:NSManagedobjectContext = appDelegate.managedobjectContext

    if editingStyle == .Delete {
        context.deleteObject(results[indexPath.row])
        context.save(nil)
    }

    results.removeAtIndex(indexPath.row)
    tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade)

}

func filterContentForSearchText (searchText: String) {
    searchResults = results.filter{
        ($0.lastname as Nsstring).localizedCaseInsensitiveContainsstring("\(searchText)")
    }
}

func searchdisplayController(controller: UISearchdisplayController!,shouldReloadTableForSearchString searchString: String!) -> Bool {
    self.filterContentForSearchText (searchString)
    return true
}

}

函数prepareForSegue条件中,self.tableView == self.searchdisplayController.searchResultsTableView未实现.

始终指定indexPath = self.tableView.indexPathForSelectedRow()而不是indexPath = self.searchdisplayController.searchResultsTableView.indexPathForSelectedRow().如果在搜索结果中选择行,则会导致错误.

链接到DropBox上的项目:https://www.dropbox.com/s/bqv46nkoa4s3ibg/lesson-12-2-swift%203.zip

self.tableView是主表视图,所以条件
if self.tableView == self.searchdisplayController.searchResultsTableView

永远不会是真的.但您可以检查搜索是否有效:

if self.searchdisplayController.active {
    // index path from search table ... 
} else {
    // index path from main table ...
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...