简体   繁体   中英

swift-Tableview cell UIButton background color

Working on a app that has a tableview. In each cell there're two buttons(Pass/fail). If the user taps on "ok" for pass background changes green and for "!" fail, the background changes red. Issue that I'm running into is that if a button is tapped on in a row(let say the fail button). Scrolling drown to another row(like 5 rows down). fail button background color also changed(red).

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: CellTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellTableViewCell

    if cell == nil
    {
        cell = CellTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.text = myArray[indexPath.row]
        cell.fail.tag = indexPath.row
        cell.fail.addTarget(self, action: #selector(ViewController.errorBtn(_:)), for: .touchUpInside)
    }
    else
    {
        cell.textLabel?.text = myArray[indexPath.row]
        cell.fail.tag = indexPath.row
        cell.fail.addTarget(self, action: #selector(ViewController.errorBtn(_:)), for: .touchUpInside)
    }

    return cell
}

here's a link to quick mockup of app with code im having issue. https://github.com/morenoa/iOS3/tree/master/Table%20Cell Any help is greatly appreciated thanks. Sorry if repost. Just stumped.

It appears that your app has no way of knowing that is should return back to green as you have only implemented a function to change it to alert/red but you are are not checking anything else so when you complete the action in your if statement then all cells will change as they do not know differently.

I hope this helps

You can track button state by changing the array to the dictionary and set value as true/false.

For Eg var myArray = ["my": false, "hello": false]

then set state in cellForRowAt cellForRowAt according to a value of a key and in func errorBtn(_ sender: UIButton) just toggle selected button value.

First, you have to keep a reference that if the button has already selected. Second, it's better to set the background color of the button on the cellForRowAt function.

For keeping a reference for the selected button can be achieve in various ways. Usually I used an array of object, which on the object got a bool attribute.

In your case, I think the simplest way is like this, since you already have an array of string that used for cell title. You can create an array of bool for keeping the button state. Such as:

var myArraySelected:[Bool] = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]

Then on the cellForRowAt put this code to set the background color of the button

if (myArraySelected[indexPath.row]){ cell.fail.backgroundColor = UIColor.red }else{ cell.fail.backgroundColor = UIColor(red: 214.0/255.0, green: 214.0/255.0, blue: 214.0/255.0, alpha: 1.0) }

Last, change the errorBtn action into:

@IBAction func errorBtn(_ sender: UIButton) { myArraySelected[(sender as AnyObject).tag] = true self.tableView.reloadData() }

Don't forget to create an outlet for the table view.

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