首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >searchDisplayController,UITableView,核心数据和Swift

searchDisplayController,UITableView,核心数据和Swift
EN

Stack Overflow用户
提问于 2014-06-30 12:13:51
回答 1查看 2.6K关注 0票数 2

试图使用searchDisplayController在表中搜索。数据过滤配置,搜索对话框工作。现在,我想使用方法prepareForSegue将当前值indexPath发送到新的UIViewController

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-30 12:40:44

self.tableView是主表视图,所以条件

代码语言:javascript
复制
if self.tableView == self.searchDisplayController.searchResultsTableView

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

代码语言:javascript
复制
if self.searchDisplayController.active {
    // index path from search table ... 
} else {
    // index path from main table ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24489926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档