Ios 滚动后TableView单元格颜色重置
我正在使用swift制作我的第一个应用程序之一,我希望能够根据天气的不同改变特定单元格的颜色,这些单元格已经被选中并添加到SelectedHabiods中。无论何时这样做,它似乎工作,但当我向下滚动并备份时,单元格似乎重置为原始颜色Ios 滚动后TableView单元格颜色重置,ios,swift,uitableview,cocoa-touch,Ios,Swift,Uitableview,Cocoa Touch,我正在使用swift制作我的第一个应用程序之一,我希望能够根据天气的不同改变特定单元格的颜色,这些单元格已经被选中并添加到SelectedHabiods中。无论何时这样做,它似乎工作,但当我向下滚动并备份时,单元格似乎重置为原始颜色 import UIKit var selectedHobbies : [String] = [] //Hold a global value of the selected hobbies var numberSelected:Int = 0 //Hold t
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
}
}
非常感谢您提供的任何帮助,谢谢您在您的单元格中查看选定的爱好检查对象是否位于选定的爱好和颜色单元格中
if selectedHobbies.contains(hobby){
}else{
}
你需要在cellForRowAt中设置颜色。我不完全确定你的意思,在哪里插入给定的代码行?在cellForRowAt哪里可以修改cellForRowAt。抱歉,我对一般编程非常陌生。重写func tableView\utableview:UITableView,cellForRowAt indexPath:indexPath->UITableViewCell{像这样?重写func tableView\utableview:UITableView,cellForRowAt indexPath:indexPath->UITableViewCell{如果选择了嗜好。containshobby{cell.backgroundColor=UIColor.yellow}其他{cell.backgroundColor=UIColor.clear}返回单元格}