简体   繁体   English

一旦按下单独的UIButton,则禁用UITableViewCell中的所有UIButton

[英]Disable all UIButtons in UITableViewCell once a separate UIButton is pressed

Problem I've found some questions asking how to disable a particular cell button in a table view, but what I want to do is to disable all instances of a button within a table view cell when another button is pressed. 问题我发现了一些问题,询问如何在表格视图中禁用特定的单元格按钮,但是我想做的是在按下另一个按钮时禁用表格视图单元格中按钮的所有实例。

Details I have a table view which is displaying a list of exercises and number of reps in a custom cell. 详细信息我有一个表视图,该视图在自定义单元格中显示练习列表和重复次数。 Within the custom cell is also a button "swap" which allows a user to swap an exercise for another one before the workout starts. 在自定义单元格中还有一个“交换”按钮,允许用户在锻炼开始之前将一项锻炼交换为另一项锻炼。

When the user hits "start workout" (which triggers a timer) I want to disable all instances of the swap button (grey them all out and make non clickable). 当用户点击“开始锻炼”(触发计时器)时,我想禁用交换按钮的所有实例(将它们全部变灰,使它们不可单击)。

Code

My workout cell class is here : 我的健身室课程在这里:

class WorkoutCell : UITableViewCell {

    var delegate: WorkoutCellDelegate?

    @IBAction func swapButtonPressed(_ sender: Any) {
        delegate?.swapButtonTapped(cell: self)
    }

    @IBOutlet weak var exerciseName: UILabel!
    @IBOutlet weak var repsNumber: UILabel!
    }
    protocol WorkoutCellDelegate {

        func swapButtonTapped(cell: WorkoutCell)

        }

What have I tried 我尝试了什么

The way I thought to do this was to add an IBOutlet (eg 'swapButton') for the button and then simply do something like : 我认为要这样做的方法是为按钮添加IBOutlet(例如'swapButton'),然后简单地执行以下操作:

   @IBAction func startButtonPressed(_ sender: UIButton) {
       WorkoutCell.swapButton.isenabled = false
    }

But Xcode doesn't allow you to add IBOutlets to repeating cells so I'm a bit stuck. 但是Xcode不允许您将IBOutlets添加到重复的单元格中,所以我有点卡住了。

I'm fairly new to delegates (managed to get it working for displaying the table view) so if it has something simple to do with that sorry! 我对代表来说还很陌生(设法使它能够显示表视图),所以对不起,如果有一些简单的事情!

Add a property to your viewcontroller: 向您的视图控制器添加属性:

var swapButtonsDisabled : Bool = false

In your cellForRow do something like this: 在您的cellForRow中执行以下操作:

cell.swapButton.isEnabled = !self.swapButtonsDisabled

When the start button is pressed, set swapButtonDisabled to true and reload the tableView. 当按下开始按钮时,将swapButtonDisabled设置为true并重新加载tableView。

1- As you connect 1-连接时

@IBOutlet weak var exerciseName: UILabel!

create outlet for every btn 为每个btn创建出口

@IBOutlet weak var btn1: UIButton!

2- Add a property to the model array in the VC to hold the state of every cell 2-向VC中的模型数组添加属性以保存每个单元格的状态

3- When you click the main btn fire the delegate method with the btn's cell 3-当您单击主btn时,使用btn的单元格启动委托方法

4- In VC delegate handle method disable the btns and change the state of the array index path 4-在VC委托句柄方法中,禁用btns并更改数组索引路径的状态

5- Don't forget to check state in cellForRow 5-不要忘记在cellForRow中检查状态

You are pretty close. 你很亲密 First I suggest you to be more specific and have the data you need in cell and use access control: 首先,我建议您更加具体,并在单元格中拥有所需的数据并使用访问控制:

class WorkoutCell : UITableViewCell {

    var workoutSwappable: (workout: Workout, canBeSwapped: Bool)? {
       didSet {
           swapButton.isEnabled = workoutSwappable?.canBeSwapped == true
           // TODO: do additional setup here
       }
    }

    weak var delegate: WorkoutCellDelegate? // Needs to be weak or you will have memory leaks

    @IBAction private func swapButtonPressed(_ sender: Any) {
        if let workoutSwappable = workoutSwappable, workoutSwappable.canBeSwapped == true {
            delegate?.workoutCell(self, didTapWorkoutSwap: workoutSwappable.workout)
        }
    }

    @IBOutlet private var exerciseName: UILabel!
    @IBOutlet private var repsNumber: UILabel!
    @IBOutlet private var swapButton: UIButton!
    }

Ok so now in cell for row at index path all you need is something like: 好了,现在在索引路径的单元格中,您需要的是:

cell.workoutSwappable = (self.items[0], self.canSwap)

On delegate you now have: 现在,您可以:

func workoutCell(_ sender: WorkoutCell, didTapWorkoutSwap workout: workout) {
    self.currentWorkout = workout
    self.canSwap = false
    self.initializeTimer()
    tableView.reloadData() // This will now flush all the buttons
}

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

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