gpt4 book ai didi

ios - 当 searchBar 文本高于 0 时删除单元格

转载 作者:行者123 更新时间:2023-11-28 07:06:51 25 4
gpt4 key购买 nike

我创建了一个 tableView,我首先将其设置为如果 searchController 处于事件状态,它应该使用过滤后的数组。但是,当 searchBar 包含超过 0 个字符时,我想首先执行它。但是,这似乎不起作用。这是我到目前为止在 cellForRowAtIndexPath 中所做的

    if (count(self.teamSearchController.searchBar.text) > 0) {
team = filteredTableData[indexPath.row] as Team
cell.textLabel?.text = team.name as String

} else {
team = self.teamArray[indexPath.row] as Team
cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20)
cell.textLabel?.text = team.name as String

}

updateSearchResultsForSearchController

func updateSearchResultsForSearchController(searchController: UISearchController)
{



filteredTableData.removeAll(keepCapacity: false)

let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR shortname CONTAINS[c] %@", searchController.searchBar.text, searchController.searchBar.text)
let array = (teamArray as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [Team]
filteredTableData = array

self.tableView.reloadData()
}

最佳答案

您应该在 updateSearchResultsForSearchController 中检查搜索文本的长度

func updateSearchResultsForSearchController(searchController: UISearchController)
{

if searchController.searchBar.text == "" {
filteredTableData = self.teamArray
} else {
filteredTableData.removeAll(keepCapacity: false)

let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR shortname CONTAINS[c] %@", searchController.searchBar.text, searchController.searchBar.text)

let array = (teamArray as NSArray).filteredArrayUsingPredicate(searchPredicate) as! [Team]
filteredTableData = array
}
self.tableView.reloadData()
}

更新 cellForRowAtIndexPath 中的代码,如下所示,只需检查 searchController 是否处于事件状态

if (self.teamSearchController.active) {
team = filteredTableData[indexPath.row] as Team

} else {
team = self.teamArray[indexPath.row] as Team
cell.textLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 20)
}
cell.textLabel?.text = team.name as String

关于ios - 当 searchBar 文本高于 0 时删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30226392/

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