这是我的头疼...希望有人吃药。
情况-我有一个搜索栏链接到下面的表格视图-Tableview单元由1个标签和1个按钮组成-该按钮是链接到coredata的“收藏夹”按钮-如果标签文字确实存在于coredata中,则其颜色为棕色-如果文本不存在于coredata中,则其颜色为灰色。如果按下该按钮,它将变成棕色,并且该标签的文本已注册在coredata中]
头痛-一切都非常适合第一次过滤(一切都准备就绪,无论是否注册文本,按钮颜色都会正确显示,并且通过单击按钮进行注册非常完美,但是...-当搜索更改时,将完全(取消)过滤单元格,但不会过滤按钮:应显示为灰色的单元格显示为棕色-如果我更改屏幕并返回所有内容,并且按钮的颜色都可以,就可以了
我找不到在搜索过程中如何刷新按钮标题颜色的方法。 Buttons的标题是文本。有提示吗?
CellForRowAt中的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = searchTbv.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath) as! SearchTbvCell
if searhing {
cell.searchLbl.text = mySearch[indexPath.row]
}
else {
cell.searchLbl.text = dataSearch.searchItem[indexPath.row]
}
return cell
}
搜索栏的代码
extension SearchVC: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searhing = true
mySearch = dataSearch.searchItem.filter({$0.lowercased().folding(options: .diacriticInsensitive, locale: .current).prefix(searchText.count) == searchText.lowercased()})
searchTbv.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searhing = false
searchBar.text = ""
searchTbv.reloadData()
}
设置按钮颜色的代码(位于自定义单元格类中的代码:
class SearchTbvCell: UITableViewCell {
//MARK: OUTLETS
@IBOutlet weak var searchLbl: UILabel!
@IBOutlet weak var searchBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
fetchFromCoreData()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
fetchFromCoreData()
}
@IBAction func favoriteBtnTapped(_ sender: UIButton) {
// Set fav button color to brown if selected/tapped
searchBtn.setTitleColor(.brown, for: .normal)
// Permanently record selection as favorite into coredata
recordPermanently()
}
//MARK: METHODS
// Fetch contents from CoreData and up-date button's color consequently
func fetchFromCoreData () {
// Create a reference to container
let appDelegate = UIApplication.shared.delegate as? AppDelegate
// Create a context from the container
let managedContext = appDelegate?.persistentContainer.viewContext
// Prepare a request and sorting
let myRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "MyFavorites")
myRequest.predicate = NSPredicate(format:"favoriteWines = %@", searchLbl.text!)
do {
let results = (try managedContext?.fetch(myRequest))!
for _ in results {
searchBtn.setTitleColor(.brown, for: .normal)
}
}
catch{}
}
// Record permanently in core data and changethe button's color
func recordPermanently(){
// Create a reference to container
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
// Create a context from the container
let managedContext = appDelegate.persistentContainer.viewContext
// Create an entity to manage records
let recordEntity = NSEntityDescription.entity(forEntityName: "MyFavorites", in: managedContext)!
// Create a new record
let newRecord = NSManagedObject(entity: recordEntity, insertInto: managedContext)
// Pass values to new record
newRecord.setValue(searchLbl.text!, forKeyPath: "favoriteWines")
// Save record
do {
try managedContext.save()
searchBtn.setTitleColor(.brown, for: .normal)
}
catch let error as NSError{
print("Failed to save permanently. \(error), \(error.userInfo)" )
}
}
}
免责声明:在浏览器中键入,未在Xcode中测试
您尚未指示要设置按钮颜色的位置和方式,但是我建议您在willDisplayCell
中进行设置:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.label.text.existsInCoreCata() {
cell.button.setTitleColor(.brown, for: .normal)
}
else {
cell.button.setTitleColor(.grey, for: .normal)
}
}
existsInCoreCata()
是您的代码,在其中检查标签是否存在于CoreData中。