Jeyanth
Jeyanth

Reputation: 59

Disabling the user interaction of all subviews within a tableview

I have a tableview and a view beneath it for buttons. I want to disable the user interaction of all the subviews within the tableview, however scrolling of the tableview should not be disabled. This is required on clicking of 'Preview' button. The button view should not be disabled. Suggestions welcome. i tried the following. but this doesn't enable the scrolling nor the button interactions.

    - (IBAction)btnPreviewTapped:(id)sender {
for (UIView *eachView in self.view.superview.subviews) {
    if (eachView != formsContentTableView) {
        eachView.userInteractionEnabled = NO;
    }
}
formsContentTableView.userInteractionEnabled = YES;
self.buttonView.userInteractionEnabled = YES;
[self.btnPreview setTitle:@"Submit" forState:UIControlStateNormal];

}

Upvotes: 1

Views: 1458

Answers (2)

Jeyanth
Jeyanth

Reputation: 59

found another solution based on what was suggested, whick will not work for disabling user interaction for components within content view, but will disable the didSelectRowAtIndexPath, which was also expected.

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   if(self.bIsPreview)
      return nil;
 else
return indexPath;
}

Upvotes: 0

Lucifer
Lucifer

Reputation: 480

I would create a property @property (nonatomic, assign) BOOL isPreview;, assign it NO as initial value self.isPreview = NO; and then in the -tableView:cellForRowAtIndexPath: when you create your cell subviews, I will check

if(self.isPreview) { 
    subview1.userInteractionEnabled = NO; 
    subview2.userInteractionEnabled = NO;
    // ... etc 
}

and then set in your IBAction as follows:

-(IBAction)btnPreviewTapped:(id)sender {
    if(self.isPreview) {
        self.isPreview = NO;
    } else {
        self.isPreview = YES;
    }
    [self.tableView reloadData];
}

-reloadData method will redraw the visible cells and their subviews, but depending on self.isPreview the subviews will or will not react on user's touch.

Upvotes: 1

Related Questions