Ios 如何从包含搜索控制器结果的表中进行分离
旧的UISearchDisplayController类现在已被弃用,我们必须使用新的UISearchController。旧类中曾经有一个名为“SearchResultsTableView”的属性,但它已从新类中消失 我用数据填充了一个表,所有的工作都按预期进行——包括将每一行的细节分割到另一个场景。我在那里抛出了一个搜索栏(以编程方式-使用新的searchController),它成功地用找到的任何结果重新加载原始表。 但是,在搜索后触摸选定行时,传递的序列是原始表行的序列,该行恰好与现在触摸的行位于同一位置!(即,如果我选择搜索的当前第二行,下一个场景将分割原始表第二行的详细信息!)这是因为尽管行中的数据成功地重新填充了搜索数据,但索引号仍然是旧数据的索引号 过去,我们会对旧类型进行如下检查:Ios 如何从包含搜索控制器结果的表中进行分离,ios,xcode,swift,uisearchcontroller,Ios,Xcode,Swift,Uisearchcontroller,旧的UISearchDisplayController类现在已被弃用,我们必须使用新的UISearchController。旧类中曾经有一个名为“SearchResultsTableView”的属性,但它已从新类中消失 我用数据填充了一个表,所有的工作都按预期进行——包括将每一行的细节分割到另一个场景。我在那里抛出了一个搜索栏(以编程方式-使用新的searchController),它成功地用找到的任何结果重新加载原始表。 但是,在搜索后触摸选定行时,传递的序列是原始表行的序列,该行恰好与现在触
if (self.resultSearchController.active) {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()
} else {
let indexPath = self.tableView.indexPathForSelectedRow()
因此,我认为使用旧的UISearchDisplayController类,您实际上得到了一个新表,而使用新的SearchController类,您只得到旧表中的新行?这完全没有道理
以下是每个请求的完整代码:
import UIKit
import Foundation
class secondTableViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
var filteredTableData = [String]()
var resultSearchController = UISearchController()
//these 2 are standard for the title and subtitle
var TableTitle:Array< String > = Array < String >()
var TableSub:Array< String > = Array < String >()
//the following are for my seque to next scene
var the_fname:Array< String > = Array < String >()
var the_basics:Array< String > = Array < String >()
var the_p_method:Array< String > = Array < String >()
var the_seats:Array< String > = Array < String >()
var the_notes:Array< String > = Array < String >()
var the_tableData:Array< String > = Array < String >()
override func viewDidLoad() {
tableView.delegate = self
tableView.dataSource = self
self.title = currentBus
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
var url = "http://the_path_to_my_json_file"
get_data_from_url(url)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else {
return TableTitle.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("secondtableCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
//cell.detailTextLabel?.text = TableSub[indexPath.row]
}else{
cell.textLabel?.text = TableTitle[indexPath.row]
cell.detailTextLabel?.text = TableSub[indexPath.row]
}
return cell
}
func get_data_from_url(url:String)
{
let httpMethod = "GET"
let timeout = 15
let url = NSURL(string: url)
let urlRequest = NSMutableURLRequest(URL: url!,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 15.0)
let queue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(
urlRequest,
queue: queue,
completionHandler: {(response: NSURLResponse!,
data: NSData!,
error: NSError!) in
if data.length > 0 && error == nil{
let json = NSString(data: data, encoding: NSASCIIStringEncoding)
self.extract_json(json!)
}else if data.length == 0 && error == nil{
println("Nothing was downloaded")
} else if error != nil{
println("Error happened = \(error)")
}
}
)
}
func extract_json(data:NSString)
{
var parseError: NSError?
let jsonData:NSData = data.dataUsingEncoding(NSASCIIStringEncoding)!
let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &parseError)
if (parseError == nil)
{
if let my_pass_list = json as? NSArray
{
for (var i = 0; i < my_pass_list.count ; i++ )
{
if let each_pass = my_pass_list[i] as? NSDictionary
{
if let fname = each_pass["fname"] as? String
{
if let lname = each_pass["lname"] as? String
{
if let numofseats = each_pass["numofseats"] as? String
{
if let showed_up = each_pass["showed_up"] as? String
{
if let res_id = each_pass["resnum"] as? String
{
if let res_notes = each_pass["res_notes"] as? String
{
if let payment_description = each_pass["payment_description"] as? String
{
// the_tableData.append(fname)
the_fname.append(fname)
the_basics.append(fname + " " + lname)
the_p_method.append(payment_description)
the_seats.append(numofseats)
the_notes.append(res_notes)
TableTitle.append(fname + " " + lname)
TableSub.append("Seats Reserved: " + numofseats + ". Showed Up: " + showed_up + ". Notes:" + res_notes)
the_tableData = TableTitle
}
}
}
}
}
}
}
}
}
}
}
do_table_refresh();
}
func do_table_refresh()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
var thirdScene = segue.destinationViewController as! customer_details_View_Controller
if let indexPath = self.tableView.indexPathForSelectedRow() {
/*
so what I'm missing is to be able to check
if (self.resultSearchController.active) {
and if yes have indexPath be the self.resultSearchController.resultSearchTableView.indexPathForSelectedRow() {
or something of that nature
*/
thirdScene.dotrav = todayString
thirdScene.from = currentBus
thirdScene.basics = the_basics[indexPath.row]
thirdScene.p_method = the_basics[indexPath.row]
thirdScene.seats = the_tableData[indexPath.row]
thirdScene.notes = the_notes[indexPath.row]
}
// Pass the selected object to the new view controller.
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
let array = (the_tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
}
导入UIKit
进口基金会
类secondTableViewController:UITableViewController、UITableViewDelegate、UITableViewDataSource、UISearchResultsUpdated{
变量filteredTableData=[String]()
var resultSearchController=UISearchController()
//这两个是标题和副标题的标准
变量TableTitle:Array=Array()
var TableSub:Array=Array()
//以下是我下一个场景的顺序
变量名称:数组=数组()
var_basics:Array=Array()
var_p_方法:Array=Array()
数组=数组()
var the_notes:Array=Array()
var_tableData:Array=Array()
重写func viewDidLoad(){
tableView.delegate=self
tableView.dataSource=self
self.title=currentBus
super.viewDidLoad()
self.resultSearchController=({
let controller=UISearchController(searchResultsController:nil)
controller.searchResultsUpdater=self
controller.dimsBackgroundDuringPresentation=false
controller.searchBar.sizeToFit()文件
self.tableView.tableHeaderView=controller.searchBar
返回控制器
})()
//重新加载表格
self.tableView.reloadData()
变量url=”http://the_path_to_my_json_file"
从url(url)获取数据
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//标记:-表视图数据源
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
//#警告可能不完整的方法实现。
//返回节数。
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
// 2
if(self.resultSearchController.active){
返回self.filteredTableData.count
}
否则{
返回TableTitle.count
}
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell=self.tableView.dequeueReusableCellWithIdentifier(“secondtableCell”,forIndexPath:indexPath)作为!UITableViewCell
//配置单元格。。。
if(self.resultSearchController.active){
cell.textLabel?.text=filteredTableData[indexPath.row]
//cell.detailTextLabel?.text=TableSub[indexPath.row]
}否则{
cell.textLabel?.text=TableTitle[indexPath.row]
cell.detailTextLabel?.text=TableSub[indexPath.row]
}
返回单元
}
func从url获取数据(url:String)
{
让httpMethod=“GET”
让超时=15
让url=NSURL(字符串:url)
让urlRequest=NSMutableURLRequest(URL:URL!,
cachePolicy:。重新加载IgnoringLocalAndRemoteCacheData,
timeoutInterval:15.0)
let queue=NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(
请求,
队列:队列,
completionHandler:{(响应:NSURLResponse!,
数据:NSData!,
错误:n错误!)在中
如果data.length>0&&error==nil{
让json=NSString(数据:数据,编码:NSASCIIStringEncoding)
self.extract_json(json!)
}否则,如果data.length==0&&error==nil{
println(“未下载任何内容”)
}否则,如果错误!=nil{
println(“发生错误=\(错误)”)
}
}
)
}
func extract_json(数据:NSString)
{
var解析错误:N错误?
让jsonData:NSData=data.dataUsingEncoding(NSASCIIStringEncoding)!
让json:AnyObject?=NSJSONSerialization.JSONObjectWithData(jsonData,选项:nil,error:&parseError)
if(parseError==nil)
{
如果让我的\u传递\u list=json作为?NSArray
{
对于(var i=0;iclass MyTableViewController: UITableViewController, UISearchResultsUpdating {
var data: [String] = ["One", "Two", "Three", "Four", "Five"]
var filteredData: [String]!
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
setUpSearchController()
setFilteredDataForCurrentSearch()
}
private func setUpSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
self.tableView.tableHeaderView = searchController.searchBar
}
private func setFilteredDataForCurrentSearch() {
if let searchString = searchController.searchBar.text where !searchString.isEmpty {
filteredData = data.filter({ (string: String) -> Bool in
return searchString.rangeOfString(string, options: NSStringCompareOptions.CaseInsensitiveSearch) != nil
})
} else {
filteredData = data
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
setFilteredDataForCurrentSearch()
}
}
let indexPath = tableView.indexPathForSelectedRow()
let selectedObject = filteredData[indexPath.row]