0

I m using a UISearchController to let the user search though my data (these comes from XML and been parsed to arrays), now i want to add more then one UILabel into my Cell, but these UILabel Information comes from different arrays.

As soon as i update my Search, the 2. array does not get sorted the same way like the first does.

AP = ["FIRST1","FIRST2","FIRST3","FIRST4","FIRST5"]
APSecound = ["SEC1","SEC2","SEC3","SEC4","SEC5"]
var filteredTableDataAP = [String]()
var filteredTableDataAPSecound = [String]()


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

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (AP as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredTableDataAP = array as! [String]
    //i think here i some how could set index of "filteredTableDataAP = filteredTableDataAPSecound" so they been filtered like the same shema



    self.tableView.reloadData()
}


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


    if (self.resultSearchController.active) {
        cell.APLB.text = filteredTableDataAP[indexPath.row]
        cell.APSecoundLB.text = filteredTableDataAPSecound[indexPath.row]

        return cell
    }
    else {
        cell.APLB.text = AP[indexPath.row]
        cell.APSecoundLB.text = APSecound[indexPath.row]

        return cell
    }
}

On first run without searching the cell looks fine like

(FIRST1)(SEC1)
(FIRST2)(SEC2)
(FIRST3)(SEC3)
(FIRST4)(SEC4)

But once i search for "4" the cell looks like

(FIRST4)(SEC1)

is there any easy way to "keep the position of the second array like the one of the first"

2 Answers 2

1

The way i have used is below

you have two array :

        AP = ["FIRST1","FIRST2","FIRST3","FIRST4","FIRST5"]
        APSecound = ["SEC1","SEC2","SEC3","SEC4","SEC5"]
        var getIndex = 0

        if (self.SearchController.active) // check if SearchController isactive
        {

            //This will get the index of main array
             getIndex = AP.indexOf(filteredsearchPost[indexPath.row])! 
            //According to example index should be 1 for "FIRST2"


    .      cell.APSecoundLB.text = APSecound[getIndex] 
//set it with the retrived index and its done
//it will print "SEC2"


        }else {
           getIndex = indexPath.row
          cell.APLB.text = AP[getIndex]
          cell.APSecoundLB.text = APSecound[getIndex]
          //Do the else part here
      }
Sign up to request clarification or add additional context in comments.

Comments

1

you just filtered the AP not filtered APSecound

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

 filteredTableDataAP = array as! [String]
filteredTableDataAPSecound = arraySecond as! [String]

or you can use filteredResault: [[String: String?]] , [String: String?] is the Dictionary, filteredResault is Dictionary array. dict like ["AP": "FIRST1", "APSecound": "SEC1"]

1 Comment

He doesn't want to search in second array ... So it won't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.