gpt4 book ai didi

ios - 在 Swift 中为 UISearchController 链接数组

转载 作者:行者123 更新时间:2023-11-28 12:58:58 27 4
gpt4 key购买 nike

我正在使用 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)

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

最佳答案

下面是我用过的方法

你有两个数组:

        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
}

关于ios - 在 Swift 中为 UISearchController 链接数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com