swift-Tableview cell UIButton background color


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.





You should check if the button is already selected or not in cellForRowAt and then change the background color accordingly.
– Pankaj
2 days ago




3 Answers
3



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.


cellForRowAt



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]


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


cellForRowAt



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)
}


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()
}


@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.



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 E.g var myArray = ["my": false, "hello": false]


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.


cellForRowAt


func errorBtn(_ sender: UIButton)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV