首页 > 解决方案 > 滚动后 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




    }


 }

非常感谢任何帮助,谢谢

标签: iosswiftuitableviewcocoa-touch

解决方案


在您cellForRowAtselectedHobbies 中检查对象是否在 selectedHobbies 中并根据该颜色为 Cell

   if selectedHobbies.contains(hobby){

     }else{

   }

推荐阅读