【问题标题】:Link arrays for UISearchController in SwiftSwift 中 UISearchController 的链接数组
【发布时间】:2016-03-24 16:46:24
【问题描述】:

我正在使用 UISearchController 让用户搜索我的数据(这些来自 XML 并被解析为数组),现在我想在我的 Cell 中添加多个 UILabel,但这些 UILabel 信息来自不同的数组。

当我更新搜索时,2. 数组的排序方式与第一个不同。

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
    }
}

第一次运行时不搜索单元格看起来很好

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

但是一旦我搜索“4”,单元格看起来像

(FIRST4)(SEC1)

有什么简单的方法可以“保持第二个数组的位置像第一个数组一样”

【问题讨论】:

    标签: ios arrays swift search


    【解决方案1】:

    我使用的方式如下

    你有两个数组:

            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
          }
    

    【讨论】:

      【解决方案2】:

      你刚刚过滤了AP 没有过滤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]
      

      或者你可以使用filteredResault: [[String: String?]][String: String?]DictionaryfilteredResaultDictionary array。像["AP": "FIRST1", "APSecound": "SEC1"]这样的字典

      【讨论】:

      • 他不想在第二个数组中搜索......所以它不会工作
      猜你喜欢
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多