简体   繁体   中英

How can I change my CustomCell background color with an in-cell UIButton

Hello I'm only 13 and live in Germany so I apologize in advance for my bad English.

I would like to change the color of the respective CustomCell with a click on a UIButton integrated in the Cell

I have tried everything I know but it has never worked properly.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var TableView: UITableView!
    @IBOutlet weak var SearchBar: UISearchBar!
    @IBOutlet weak var Banner: GADBannerView!
    @IBOutlet weak var MarkButton: UIButton! 

I will change the Color with this Button "MarkButton"

var Array = ["1","2","3","4","5","6","7","8","9","10"]

var indexSearch = 0
var Filter = [String]()
var isSearching = false

override func viewDidLoad() {
    super.viewDidLoad()
    TableView.delegate = self
    TableView.dataSource = self
    SearchBar.delegate = self
    SearchBar.returnKeyType = UIReturnKeyType.done
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isSearching {
        return Filter.count
    }else {
        return Array.count
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 55
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = TableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
    cell.CellLabel.text = Array[indexPath.row]
    cell.selectionStyle = .none

    if isSearching {
        cell.CellLabel.text = Filter[indexPath.row]
    }else {
        cell.CellLabel.text = Array[indexPath.row]
    }

    return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if SearchBar.text == nil{
        isSearching = false
        view.endEditing(true)
        TableView.reloadData()
    }else {
        isSearching = true
        searchBar.showsCancelButton = true
        Filter = Array.filter({$0.contains(searchBar.text!)})
        TableView.reloadData()
    }
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    isSearching = false
    searchBar.text = nil
    searchBar.showsCancelButton = false
    searchBar.endEditing(true)
    TableView.reloadData()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if isSearching {
        indexSearch = Array.index(of:Filter[indexPath.row])!
    }else {
        indexSearch = Array.index(of:Array[indexPath.row])!
    }

    switch indexSearch {
    case 0:
        print("0")
    case 1:
        print("1")
    case 2:
        print("2")
    case 3:
        print("3")
    case 4:
        print("4")
    case 5:
        print("5")
    case 6:
        print("6")
    case 7:
        print("7")
    case 8:
        print("8")
    case 9:
        print("9")
    case 10:
        print("10")

BROAD STEPS. (there should be a tutorial out there and you should use it too cause this is too broad for StackOverflow)

1) your customCell class needs to have an IBAction that your button can call
2) you need to hook up the cell's button to an IBAction so you can get its click
3) you tell the action to the tableview's delegate... eg via a custom delegate protocol (-customCellWasClicked(self))
4) dataSource needs to safe the information and then you call reloadData (can be optimized to not reload)
5) in cellForRow method adapt the cell's backgroundColor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM