2016-07-20 3 views
0

私はsearchControllerの値を取り込む配列を持っていなければなりません。searchControllerの結果として異なる配列から2つの値をロードするには?

let textLabel = ["Uni", "Uni", "Faber-Castell", "Faber-Castell","Faber-Castell", "Pilot", "Pilot"] 
let detailTextLabel = ["Pen", "Pencil", "Crayon", "Mechanical Pencil", "Contour Pencil", "Eraser", "Sharpener"] 

これら二つの配列対UITableViewController.同様(ユニ、PEN)、(ユニ、鉛筆)、(ファーバーカステル、クレヨン)のデータをリロードしながら...ペアで

、最初のものですセルのタイトル、2番目はサブタイトルです。私はiOSCreator's tutorialに続いた。

私の問題は、テキストを検索するときにタイトルセクションのみが更新され、サブタイトルは予想通りに更新されないということです。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell 

    // 3 
    if (self.resultSearchController.active) { 
    // it only updates textLabel. But when I add detailTextLabel 
    // subtitles comes wrong because first array contains one element 
    // more than twice. 
     cell.textLabel?.text = filteredTableData[indexPath.row] 

     return cell 
    } else { 
     cell.textLabel?.text = tableData[indexPath.row] 

     return cell 
    } 
    } 

    func updateSearchResultsForSearchController(searchController: UISearchController) { 
    filteredTableData.removeAll(keepCapacity: false) 

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text) 
    let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate) 
    filteredTableData = array as! [String] 

    self.tableView.reloadData() 
    } 
+0

は、AppleのUISearchControllerのソースコードをチェックしてください区切ら。 –

答えて

-1

は次のように単一のアレイを使用します。

let penList = ["Uni:Pen", "Uni:Pencil", "Faber-Castell:Crayon"] 

let result = filteredTableData[indexPath.row] // example: "Uni:Pen" 
let clearResult = String(result.characters.map { $0 == ":" ? " " : $0 }) 
// clearResult like this "Uni Pen" 
cell.textLabel?.text = clearResult 

また、あなたが得ることができるデータは

let result = filteredTableData[indexPath.row] 
let resultArray = result.componentsSeparatedByString(":") 
let firstText = resultArray[0] // title text 
let secondText = resultArray[1] // subtitle text 
+0

あなたは字幕をスキップしたと思います。 –

+0

これは良い解決策ではありません。とにかく後で分割する必要のある異なる値を含む文字列を人為的に作成しています(これは高価で複雑です)。 structs/classes/tuplesを使用して、代わりに値のペアを保持してください。 – Moritz

+0

しかし値が大きすぎるとどうなりますか?私は手で入力することができない約千の値を意味します。 @EricD –

関連する問題