gpt4 book ai didi

uitableview - searchDisplayController、UITableView、Core Data 和 Swift

转载 作者:可可西里 更新时间:2023-11-01 01:06:51 25 4
gpt4 key购买 nike

尝试使用 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为主 TableView ,所以条件

if self.tableView == self.searchDisplayController.searchResultsTableView

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

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

关于uitableview - searchDisplayController、UITableView、Core Data 和 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24489926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com