在 uitableview swift 3 中跟踪多个 uibutton 状态 [英] keeping track of multiple uibutton states in uitableview swift 3

查看:26
本文介绍了在 uitableview swift 3 中跟踪多个 uibutton 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,该应用程序在适用视图中每行都有一个按钮,该代码如下.

i am making an app that has a button per row in a uitable view and the for that code is as follows.

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Int(numberOfButtonsNeeded!)!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cellIdentifier = "lightCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MyTableViewCell


    cell.lightButton.addTarget(self, action: #selector(buttonPressed), for: .touchDown)
    cell.lightButton?.tag = tags[indexPath.row]

    return cell
}

基本上我想要的只是一个执行 2 个功能的按钮,我现在想跟踪该按钮以及它是否被按下.

essentially all i want is one button that performs 2 functions and i want to now keep track of the button and whether or not it has been pressed.

如果按钮已被按下,我希望它显示名为on.jpg"的特定图像并执行特定操作.如果没有按下按钮,我希望它显示off.jpg"并执行不同的操作.

if the button has been pressed i would like it to show a certain image called "on.jpg" and perform a certain action. if the button has not been pressed i would like it to show "off.jpg" and to perform a different action.

按钮应该处于两种状态之一(按下或未按下),并且不应该有中间状态.

the button should be in either one of the two states (pressed or not pressed) and there should be no intermediate state.

我的按钮按下方法如下:

my button pressed method is as follows:

func buttonPressed(_ sender : UIButton){

     if ("certain condition is met"){
            guard let image = UIImage(named: "on.jpg") else {
                print("Image Not Found")
                return
            }
            sender.setImage(image, for: UIControlState.normal)
        }

     else if ("another condition is met"){
            guard let image = UIImage(named: "off.jpg") else {
                print("Image Not Found")
                return
            }
            sender.setImage(image, for: UIControlState.normal)
        }
    }

我曾尝试使用分配给每个按钮的标签和变量来尝试这样做,但它变得太复杂了,肯定有更简单的方法来跟踪.

I've tried using tags and variables assigned to each button to try and do it but it just becomes too complicated and there must surely be an easier way to keep track.

最后,我将如何刷新 tableview 并确保所有状态始终同步

Lastly, how would i go about refreshing the tableview and making sure that all the states are always synchronized

推荐答案

您必须在表数据源中添加一个布尔变量并设置其默认值,即真或假.在单击按钮时,您可以像这样执行代码

You have to add a boolean variable in your table dataSource and sets its default value which is true of false. While on button click you can do your code like this

func buttonPressed(_ sender : UIButton){

     let isPressed = dataSource[sender.tag].isButtonPressed
    if isPressed {
       /// Button Already Pressed
    } else {
      /// Button is not pressed
    }
    dataSource[sender.tag].isButtonPressed = !isPressed
}

此外,您与代表保持不变.

Furthermore you table delegates with remain same.

这篇关于在 uitableview swift 3 中跟踪多个 uibutton 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆