Evan Johnson
Evan Johnson

Reputation: 1454

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. 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. I am not synthesizing a UIButton since I create them dynamically, so I am unable to reference the particular UIButtons. How can I disable and enable all UIButtons? I know the tags of the UIButtons, if that helps. The tags equal the indexPath.row.

Thank you in advance, Evan

Upvotes: 1

Views: 1636

Answers (2)

ljkyser
ljkyser

Reputation: 1019

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];. You would then alter the cellForRowAtIndexPath method to set each button's enabled property to the property you stored previously (enabled/disabled).

Upvotes: 0

saadnib
saadnib

Reputation: 11145

You can do it by getting subviews of your UITableViewCell as -

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];
        }
    }
}

Upvotes: 4

Related Questions