程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了searchDisplayController,UITableView,Core Data和Swift大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决searchDisplayController,UITableView,Core Data和Swift?

开发过程中遇到searchDisplayController,UITableView,Core Data和Swift的问题如何解决?下面主要结合日常开发的经验,给出你关于searchDisplayController,UITableView,Core Data和Swift的解决方法建议,希望对你解决searchDisplayController,UITableView,Core Data和Swift有所启发或帮助;

self.tableVIEw 是主表格视图,因此条件

if self.tableVIEw == self.searchdisplayController.searchResultstableVIEw

永远不会是真的。但是您可以检查搜索是否处于活动状态:

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

解决方法

尝试使用进行表格搜索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: @R_197_10495@ng) {
    searchResults = results.filter{
        ($0.lastname as NS@R_197_10495@ng).localizedCaseInsensitiveContains@R_197_10495@ng("\(searchText)")
    }
}

func searchDisplayController(controller: UISearchDisplayController!,shouldReloadTableForSearch@R_197_10495@ng search@R_197_10495@ng: @R_197_10495@ng!) -> Bool {
    self.filterContentForSearchText (search@R_197_10495@ng)
    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

大佬总结

以上是大佬教程为你收集整理的searchDisplayController,UITableView,Core Data和Swift全部内容,希望文章能够帮你解决searchDisplayController,UITableView,Core Data和Swift所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。