简体   繁体   English

启用/禁用 Tableview 单元格 - iOS - Swift

[英]Enable/Disbale Tableview cell - iOS - Swift

I have a table view loaded with 4 rows inside.我有一个表视图,里面有 4 行。

Row 3 will have a checkbox.第 3 行将有一个复选框。 I want to enable Row 4 only when Row3 check box is checked我只想在选中 Row3 复选框时启用 Row 4

so I have an outlet for checkbox (which is button) and action on tap of that checkBox.所以我有一个复选框(这是按钮)的出口和点击那个复选框的动作。

My question is how can i get reference to next cell and make it enable/diable我的问题是如何获得对下一个单元格的引用并使其启用/禁用

I tried to get reference of that cell as myTableView.cellForRow(at: index + 1) but it throws error that Binary operand cannot be applied to indexPath.我试图将该单元格的引用作为myTableView.cellForRow(at: index + 1)但它抛出错误,即二进制操作数不能应用于 indexPath。

Pls suggest how i can achieve请建议我如何实现

I assume you have your own custom UITableViewCell class.我假设您有自己的自定义UITableViewCell类。 And your MyTableViewController is an delegate for this tableview cell.而你的MyTableViewController是这个 tableview 单元格的代表。 Whenever user check/uncheck, your box delegate tells your controller about it and you invoke reload method to get rid of given row.每当用户选中/取消选中时,您的框委托都会告诉您的控制器有关它的信息,并且您调用 reload 方法来摆脱给定的行。

That's solution I am using in my app.这是我在我的应用程序中使用的解决方案。

import Foundation
import UIKit

class MyTableViewController: UITableViewController {
    private struct CurrentSetting {
        var RowAEnabled: Bool = true
        var RowBEnabled: Bool = true
        var RowCEnabled: Bool = true
        var RowDEnabled: Bool = true
        var RowEEnabled: Bool = true
        //...
        //maybe more?
    }


    /// Let's say those are your table view rows. Data or whatever.
    fileprivate var settings: [String] {
        //In here you have your static position.
        var configuration = ["Row A","Row B", "Row C"]
        //However, if some data are enabled/disabled you modify it based on settings
        if current.RowAEnabled {
            configuration += ["Row D"]
        }
        return configuration
    }
    //This is your setting for current tableview
    private var current = CurrentSetting()
}

//Use this to reload row for your enable/disable cell.
tableView.reloadRows(at: [indexPath], with: .none)

Example how did that work with my app.示例如何与我的应用程序一起使用。 IMPORTANT: settings it's data source for your tableview.重要提示:设置它是 tableview 的数据源。 number of items.东西的个数。

在此处输入图片说明

In action of that checkBox, you can perform reloadData of your tableview.在该复选框的操作中,您可以执行 tableview 的reloadData After that, you can check inside cellForRowAtIndexPath之后,您可以检查cellForRowAtIndexPath

like:喜欢:

if indexpath.row == 4  
then  
//just my opinion, you can do whatever you want.  
cell.userInteractionEnabled = checkBox.isSelected 

or you can check inside numberOfRowsInSection , you can remove it from dataSource.或者您可以检查numberOfRowsInSection内部,您可以将其从数据源中删除。

I will suggest the optimised and robust way.我会建议优化和健壮的方式。

If you have and Datasource Array to load in TableView then add one extra property named isEnabled in the Object.如果您有要在TableView加载的Datasource Array ,则在对象中添加一个名为 isEnabled 的额外属性。

Else别的

create a Array as follows创建一个Array如下

var cellStateArray = Array(repeating: true, count: numberOfRows)

then when you will toggle the check box depending on that toggle the flag for required cell index (currently it's row 4 for you) to false OR true in cellStateArray然后,当您将根据该复选框将所需单元cellStateArray引的标志(当前为您的第 4 行)切换为false或在cellStateArray true

And in the cellForRowAt Method check the flag in cellStateArray for the respective cell index and set userInteractionEnabled enable/disable (extra configuration if you want)并在cellForRowAt方法中检查cellStateArray中相应cell index cellStateArray的标志并设置 userInteractionEnabled 启用/禁用(如果需要,请额外配置)

And then simple reload the cell/table然后简单地重新加载单元格/表格

Clicking on checkBox let's say have a method name checkBoxClicked .单击 checkBox 假设有一个方法名称checkBoxClicked You can do like following:您可以执行以下操作:

@IBAction func checkBoxClicked(_ sender: Any) {
        let button = sender as! UIButton
        let clickedIndex = button.tag

        let nextIndexPath = IndexPath(item: clickedIndex+1, section: 0) // specify section here, if it's sectioned tableview
        let cell = tableView.cellForRow(at: nextIndexPath)

        // enable cell here
        cell?.isUserInteractionEnabled = true    // or reload cell
}

Do not forget to set tag to your checkbox in cellForRowAtIndexPath method like:不要忘记在 cellForRowAtIndexPath 方法中为您的复选框设置标签,例如:

cell.checkBoxButton.tag = indexPath.row

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

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