简体   繁体   English

在 TableView 中禁用/启用 UIButton

[英]Disable/Enable UIButtons in TableView

I have a table view with three sections.我有一个包含三个部分的表格视图。 The first section contains custom cells which have 3 UIButtons in each cell.第一部分包含自定义单元格,每个单元格中有 3 个 UIButton。 The content is dynamic, so I do not know how many rows or buttons there will be.内容是动态的,所以我不知道会有多少行或按钮。

I want to create a method that will disable all the UIButtons in this first section, and then another button that will enable them.我想创建一个方法来禁用第一部分中的所有 UIButton,然后创建另一个启用它们的按钮。 I am not synthesizing a UIButton since I create them dynamically, so I am unable to reference the particular UIButtons.我没有合成 UIButton 因为我动态创建它们,所以我无法引用特定的 UIButtons。 How can I disable and enable all UIButtons?如何禁用和启用所有 UIButtons? I know the tags of the UIButtons, if that helps.我知道 UIButtons 的标签,如果有帮助的话。 The tags equal the indexPath.row.标签等于 indexPath.row。

Thank you in advance, Evan提前谢谢你,埃文

You can do it by getting subviews of your UITableViewCell as -您可以通过获取 UITableViewCell 的子视图来做到这一点 -

if (indexPath.section == 0) 
{
    UITableViewCell *cellView = [tblView cellForRowAtIndexPath:indexPath];

    for (UIView *view in cellView.subviews) 
    {
        if ([view isKindOfClass:[UIButton class]]) 
        {
            UIButton *button = (UIButton*)view;
            [button setUserInteractionEnabled:TRUE];
        }
    }
}

There are multiple ways to do this.有多种方法可以做到这一点。 One simple option is to set a property indicating whether the buttons should be enabled or disabled and then calling [self.tableView reloadData];一个简单的选项是设置一个属性,指示是否应该启用或禁用按钮,然后调用[self.tableView reloadData]; . . You would then alter the cellForRowAtIndexPath method to set each button's enabled property to the property you stored previously (enabled/disabled).然后,您将更改 cellForRowAtIndexPath 方法以将每个按钮的启用属性设置为您之前存储的属性(启用/禁用)。

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

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