searchDisplayController,UITableView,Core Data和Swift

前端之家收集整理的这篇文章主要介绍了searchDisplayController,UITableView,Core Data和Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试使用searchDisplayController在表中搜索.配置数据过滤,搜索对话框有效.现在我想使用方法prepareForSegue将当前值indexPath发送到新的UIViewController:
  1. import UIKit
  2. import CoreData
  3.  
  4. class MainTableViewController: UITableViewController {
  5.  
  6. var results:AddrBook[]=[]
  7. var searchResults:AddrBook[]=[]
  8.  
  9. init(style: UITableViewStyle) {
  10. super.init(style: style)
  11. }
  12.  
  13. init(coder aDecoder: NSCoder!) {
  14. super.init(coder: aDecoder)
  15. }
  16.  
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19.  
  20. }
  21.  
  22. override func didReceiveMemoryWarning() {
  23. super.didReceiveMemoryWarning()
  24. }
  25.  
  26. override func viewDidAppear(animated: Bool) {
  27. let request = NSFetchRequest(entityName: "Person")
  28. request.returnsObjectsAsFaults = false
  29. let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
  30. let context:NSManagedObjectContext = appDelegate.managedObjectContext
  31. results = context.executeFetchRequest(request,error: nil) as AddrBook[]
  32. self.tableView.reloadData()
  33. }
  34.  
  35. override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
  36. return 1
  37. }
  38.  
  39. override func tableView(tableView: UITableView!,numberOfRowsInSection section: Int) -> Int {
  40. if tableView == self.searchDisplayController.searchResultsTableView {
  41. return searchResults.count
  42. } else {
  43. return results.count
  44. }
  45. }
  46.  
  47. override func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell! {
  48. var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
  49.  
  50. if !cell {
  51. cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: "Cell")
  52. }
  53.  
  54. if tableView == self.searchDisplayController.searchResultsTableView {
  55. cell!.textLabel.text = searchResults[indexPath.row].lastname + " " + searchResults[indexPath.row].firstname
  56. cell!.detailTextLabel.text = searchResults[indexPath.row].phonenumber
  57. } else {
  58. cell!.textLabel.text = results[indexPath.row].lastname + " " + results[indexPath.row].firstname
  59. cell!.detailTextLabel.text = results[indexPath.row].phonenumber
  60. }
  61.  
  62.  
  63.  
  64. return cell
  65. }
  66.  
  67. override func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath: NSIndexPath!) {
  68. if tableView == self.searchDisplayController.searchResultsTableView {
  69. self.performSegueWithIdentifier("editPerson",sender : self)
  70. }
  71. }
  72.  
  73. override func prepareForSegue(segue: UIStoryboardSegue!,sender: AnyObject?) {
  74.  
  75. var indexPath = NSIndexPath()
  76. if self.tableView == self.searchDisplayController.searchResultsTableView {
  77. NSLog("Trying recieve indexPath from Search")
  78. indexPath = self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow()
  79. NSLog("indexPath from Search")
  80. }
  81. else {
  82. indexPath = self.tableView.indexPathForSelectedRow()
  83. NSLog("IndexPath from main table")
  84. }
  85.  
  86. let destViewController:DetailViewController! = segue.destinationViewController as DetailViewController
  87.  
  88. if segue.identifier == "editPerson" {
  89. destViewController.receivedPerson = results
  90. destViewController.indexPath = indexPath
  91. NSLog("Selected person ID: \(results[indexPath.row].idperson)")
  92. }
  93. }
  94.  
  95. override func tableView(tableView: UITableView?,canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool {
  96. return true
  97. }
  98.  
  99.  
  100. override func tableView(tableView: UITableView!,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath!) {
  101.  
  102. let request = NSFetchRequest(entityName: "Person")
  103. request.returnsObjectsAsFaults = false
  104. let appDelegate:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
  105. let context:NSManagedObjectContext = appDelegate.managedObjectContext
  106.  
  107. if editingStyle == .Delete {
  108. context.deleteObject(results[indexPath.row])
  109. context.save(nil)
  110. }
  111.  
  112. results.removeAtIndex(indexPath.row)
  113. tableView.deleteRowsAtIndexPaths([indexPath],withRowAnimation: .Fade)
  114.  
  115. }
  116.  
  117. func filterContentForSearchText (searchText: String) {
  118. searchResults = results.filter{
  119. ($0.lastname as NSString).localizedCaseInsensitiveContainsString("\(searchText)")
  120. }
  121. }
  122.  
  123. func searchDisplayController(controller: UISearchDisplayController!,shouldReloadTableForSearchString searchString: String!) -> Bool {
  124. self.filterContentForSearchText (searchString)
  125. return true
  126. }

}

函数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是主表视图,所以条件
  1. if self.tableView == self.searchDisplayController.searchResultsTableView

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

  1. if self.searchDisplayController.active {
  2. // index path from search table ...
  3. } else {
  4. // index path from main table ...
  5. }

猜你在找的Swift相关文章