Я использую swift, чтобы сделать одно из моих первых приложений, я хочу иметь возможность изменять цвета определенных ячеек в зависимости от погоды, если они не были выбраны и добавлены в выбранные хобби.Всякий раз, когда это делается, кажется, что работает, но когда я прокручиваю вниз и назад, ячейки, кажется, сбрасываются в исходный цвет.
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
}
}
Любая помощь с благодарностью, спасибо