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 but开发者_运维技巧ton 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
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];
}
}
}
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).
精彩评论