搜索栏不刷新 UIbutton 颜色
Searchbar not refreshing UIbutton color
这是我的头痛...希望有人有药。
情况
- 我有一个搜索栏链接到下面的表格视图
- Tableview 单元格由 1 个标签和 1 个按钮组成
- 该按钮是链接到核心数据的 "favorite" 按钮
- 如果标签的文本确实存在于coredata中,它的颜色是棕色
- 如果文本不存在于 coredata 中,则其颜色为灰色。如果按下,它变成棕色,标签的文本被注册到 coredata
头痛
- 第一次过滤时一切都完美无缺(一切就绪,按钮颜色正确显示文本是否注册,并且通过单击按钮注册工作完美但......
- 当搜索改变时,单元格被完全(未)过滤但不是按钮:那些应该出现灰色的出现在棕色
- 如果我更换屏幕并返回,一切都已重置并且按钮颜色正常
我找不到如何在搜索过程中刷新按钮的标题颜色。按钮的标题是文本。有什么提示吗?
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({[=11=].lowercased().folding(options: .diacriticInsensitive, locale: .current).prefix(searchText.count) == searchText.lowercased()})
searchTbv.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searhing = false
searchBar.text = ""
searchTbv.reloadData()
}
设置按钮颜色的代码(代码位于自定义单元格 class):
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 中的代码。
我终于可以通过将 cell.searchBtn.setTitleColor(.lightGray, for: .normal) 添加到 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]
cell.searchBtn.setTitleColor(.lightGray, for: .normal) // This line has fixed the problem
if cell.searchLbl.text == myProperties.isInCoreData {
cell.searchBtn.setTitleColor(.brown, for: .normal)
}
else {
cell.searchBtn.setTitleColor(.lightGray, for: .normal)
}
}
else {
cell.searchLbl.text = dataSearch.searchItem[indexPath.row]
cell.searchBtn.setTitleColor(.lightGray, for: .normal) // This line has fixed the problem
}
return cell
}
这是我的头痛...希望有人有药。
情况 - 我有一个搜索栏链接到下面的表格视图 - Tableview 单元格由 1 个标签和 1 个按钮组成 - 该按钮是链接到核心数据的 "favorite" 按钮 - 如果标签的文本确实存在于coredata中,它的颜色是棕色 - 如果文本不存在于 coredata 中,则其颜色为灰色。如果按下,它变成棕色,标签的文本被注册到 coredata
头痛 - 第一次过滤时一切都完美无缺(一切就绪,按钮颜色正确显示文本是否注册,并且通过单击按钮注册工作完美但...... - 当搜索改变时,单元格被完全(未)过滤但不是按钮:那些应该出现灰色的出现在棕色 - 如果我更换屏幕并返回,一切都已重置并且按钮颜色正常
我找不到如何在搜索过程中刷新按钮的标题颜色。按钮的标题是文本。有什么提示吗?
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({[=11=].lowercased().folding(options: .diacriticInsensitive, locale: .current).prefix(searchText.count) == searchText.lowercased()})
searchTbv.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searhing = false
searchBar.text = ""
searchTbv.reloadData()
}
设置按钮颜色的代码(代码位于自定义单元格 class):
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 中的代码。
我终于可以通过将 cell.searchBtn.setTitleColor(.lightGray, for: .normal) 添加到 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]
cell.searchBtn.setTitleColor(.lightGray, for: .normal) // This line has fixed the problem
if cell.searchLbl.text == myProperties.isInCoreData {
cell.searchBtn.setTitleColor(.brown, for: .normal)
}
else {
cell.searchBtn.setTitleColor(.lightGray, for: .normal)
}
}
else {
cell.searchLbl.text = dataSearch.searchItem[indexPath.row]
cell.searchBtn.setTitleColor(.lightGray, for: .normal) // This line has fixed the problem
}
return cell
}