简体   繁体   English

swift-Tableview单元格UIButton背景色

[英]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). 滚动淹没到另一行(如向下5行)。 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. https://github.com/morenoa/iOS3/tree/master/Table%20Cell非常感谢您的帮助。 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. 您的应用似乎没有办法知道应返回绿色,因为您仅实现了将其更改为警报/红色的功能,但您没有检查其他任何内容,因此当您在if语句中完成操作时,然后所有的细胞都会改变,因为他们没有不同的认识。

I hope this helps 我希望这有帮助

You can track button state by changing the array to the dictionary and set value as true/false. 您可以通过将数组更改为字典并将值设置为true / false来跟踪按钮状态。

For Eg 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 cellForRowAt设置状态,在func errorBtn(_ sender: UIButton)只需切换所选按钮的值即可。

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. 通常我使用一个对象数组,该对象在对象上具有bool属性。

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 然后在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) }

Last, change the errorBtn action into: 最后,将errorBtn操作更改为:

@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. 不要忘记为表格视图创建出口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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