ios - 滚动后 TableView 单元格颜色重置
问题描述
我正在使用 swift 制作我的第一个应用程序之一,我希望能够根据天气或未选择并添加到 selectedHobbies 来更改特定单元格的颜色。每当这样做时,它似乎都可以工作,但是当我向下滚动和备份时,单元格似乎重置为原始颜色。
import UIKit
var selectedHobbies : [String] = [] //Hold a global value of the
selected hobbies
var numberSelected:Int = 0 //Hold the number of selected hobbies
class TableViewController: UITableViewController,
UISearchResultsUpdating {
var filteredHobbies = [String]() //The hobbies filted by the search bar
var searchController = UISearchController()
var resultController = UITableViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: resultController)
tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.resultController.tableView.delegate = self
self.resultController.tableView.dataSource = self
}
//updates search results according to what is in the search bar, filters hobbies out that dont contain the same string of text
func updateSearchResults(for searchController: UISearchController) {
self.filteredHobbies = hobbies.filter({ (hobbies: String) -> Bool in
if hobbies.contains(searchController.searchBar.text!)
{
return true
}
else
{
return false
}
})
self.resultController.tableView.reloadData()
}
// number of
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//swipe actions for table view
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let important = importantAction(at: indexPath)
return UISwipeActionsConfiguration(actions: [important])
}
//takes the hobby according to the searched hobbies (if they are filtered)
func importantAction(at IndexPath: IndexPath) -> UIContextualAction {
var hobby = ""
if searchController.searchBar.text! == "" {
hobby = hobbies[IndexPath.row]
} else {
hobby = filteredHobbies[IndexPath.row]
}
let action = UIContextualAction(style: .normal, title: "Important") { (action, view, completion) in
completion(true)
}
// wont add hobbies otherwise
if selectedHobbies.contains(hobby){
action.title = "Add Hobby"
action.backgroundColor = .gray
print(selectedHobbies)
return action
}else {
// adds hobbies if they arent in the array
selectedHobbies.append(hobby)
action.title = "Add Hobby"
tableView.cellForRow(at: IndexPath)?.backgroundColor = UIColor.white
action.backgroundColor = .green
numberSelected += 1
if numberSelected >= 10 {
performSegue(withIdentifier: "segue1", sender: nil)
print(selectedHobbies)
}
print(selectedHobbies)
return action
}
}
func removeAction(at IndexPath: IndexPath) -> UIContextualAction {
var hobby = ""
if searchController.searchBar.text! == "" {
hobby = hobbies[IndexPath.row]
} else {
hobby = filteredHobbies[IndexPath.row]
}
let action = UIContextualAction(style: .normal, title: "Important") { (action, view, completion) in
completion(true)
}
if selectedHobbies.contains(hobby){ //removes hobby if in selected hobbies
selectedHobbies = selectedHobbies.filter{$0 != hobby}
action.title = "Remove Hobby"
action.backgroundColor = .red
numberSelected -= 1
print(selectedHobbies)
return action
}else {
action.title = "Remove Hobby"
action.backgroundColor = .gray
print(selectedHobbies)
return action
}
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let remove = removeAction(at: indexPath)
return UISwipeActionsConfiguration(actions: [remove])
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == resultController.tableView
{
return self.filteredHobbies.count
}
else
{
return self.hobbies.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == resultController.tableView
{
cell.textLabel?.text = self.filteredHobbies [indexPath.row]
}
else
{
cell.backgroundColor = UIColor.cyan
cell.textLabel?.text = self.hobbies[indexPath.row]
}
return cell
}
}
非常感谢任何帮助,谢谢
解决方案
在您cellForRowAt
selectedHobbies 中检查对象是否在 selectedHobbies 中并根据该颜色为 Cell
if selectedHobbies.contains(hobby){
}else{
}
推荐阅读
- pandas - 处理 pandas 的“尝试使用 .iloc”错误
- amazon-web-services - 无法获取 ECR 泊坞窗图像
- php - 我尝试播种我的播种器文件,但出现此错误
- python - 如何选择 NaN 行并将 value_A 分配给新列?代码 df.loc['condition_1' & 'condition_2' & 'df.column_a == ''] = 'value_a'
- python - 如何修复'''Mul'对象在Python中没有属性'sp'”错误(使用Sympy和Math)
- git - 将几行作为单独的提交更改分阶段
- spring - 使用轮询器测试直接通道和源的错误处理
- c# - 如何在 xamarin.forms 中为图像添加点击和长按手势?
- swift - 如何从/到 URL、Swift、MacOS 裁剪 Jpeg 图像
- c++ - 如何使用关键字签名在 CMake 上链接 opengl 库