How to disable UITableViewCell?

I know about UITableview: how to disable selection for some rows, but not others, and cell.selectionStyle = UITableViewCellSelectionStyleNone , but how to create a cell (or any UIView for that matter) displays disabled (gray), as shown below?

disabled UITableViewCell

+82
uitableview appearance
May 6 '11 at
source share
8 answers

You can simply disable the cell text fields to darken them:

Swift 4.x

 cell!.isUserInteractionEnabled = false cell!.textLabel!.isEnabled = false cell!.detailTextLabel!.isEnabled = false 
+155
Nov 11
source share

Thanks to @Ajay Sharma, I figured out how to disable UITableViewCell :

 // Mac native DigitalColor Meter reads exactly {R:143, G:143, B:143}. cell.textLabel.alpha = 0.439216f; // (1 - alpha) * 255 = 143 aSwitch.enabled = NO; // or [(UISwitch *)cell.accessoryView setEnabled:NO]; 

And then, to actually disable the cell:

 cell.userInteractionEnabled = NO; 
+22
May 14 '11 at 17:59
source share

A quick extension that works well in the context in which I use it; Your mileage may vary.

Swift 2.x

 extension UITableViewCell { func enable(on: Bool) { for view in contentView.subviews as! [UIView] { view.userInteractionEnabled = on view.alpha = on ? 1 : 0.5 } } } 

Swift 3:

 extension UITableViewCell { func enable(on: Bool) { for view in contentView.subviews { view.isUserInteractionEnabled = on view.alpha = on ? 1 : 0.5 } } } 

Now this is just a call to myCell.enable(truthValue) .

+22
May 09 '15 at
source share

Try using a little trick:

Just set the alpha cells. Put some condition as your own requirements and set alpha.

 cell.alpha=0.2; 

If this does not work, how do you like it, use the second trick,

Just take an image of the size of a cell having a gray background with a transparent background, just add this image to the image based on the contents of the cell. Like this:

 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... if(indexPath.row==0) { cell.userInteractionEnabled=FALSE; UIImageView *img=[[UIImageView alloc]init]; img.frame=CGRectMake(0, 0, 320, 70); img.image=[UIImage imageNamed:@"DisableImage.png"]; img.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:img]; [img release]; } else { //Your usual code for cell interaction. } return cell; } 

Although I'm not sure about this, it will certainly fulfill your requirement. This will give the illusion in the user's mind that the cell is disabled. Just try using this solution. Hope that solves your problem.

+17
May 13 '11 at 10:48 a.m.
source share

An excellent extension from Kevin Owens, this is my correction for working with Swift 2.x :

 extension UITableViewCell { func enable(on: Bool) { self.userInteractionEnabled = on for view in contentView.subviews { view.userInteractionEnabled = on view.alpha = on ? 1 : 0.5 } } } 

Swift 3:

 extension UITableViewCell { func enable(on: Bool) { self.isUserInteractionEnabled = on for view in contentView.subviews { view.isUserInteractionEnabled = on view.alpha = on ? 1 : 0.5 } } } 
+4
Oct 09 '15 at 8:20
source share

I created the following extension for Enable / Disable UITableViewCell, it is very convenient to use. Create the UITableViewCell extension using "UITableViewCell + Ext.h".

 @interface UITableViewCell (Ext) - (void)enableCell:(BOOL)enabled withText:(BOOL)text; - (void)enableCell:(BOOL)enabled withText:(BOOL)text withDisclosureIndicator:(BOOL)disclosureIndicator; - (void)disclosureIndicator:(BOOL)disclosureIndicator; @end 

"UITableViewCell + Ext.m" contains the following.

 @implementation UITableViewCell (Ext) - (UITableView *)uiTableView { if ([[UIDevice currentDevice] systemVersionIsGreaterThanOrEqualTo:@"7.0"]) { return (UITableView *)self.superview.superview; } else { return (UITableView *)self.superview; } } - (void)enableCell:(BOOL)enabled withText:(BOOL)text { if (enabled) { self.userInteractionEnabled = YES; if (text) { self.textLabel.alpha = 1.0f; self.alpha = 1.0f; self.detailTextLabel.hidden = NO; } } else { self.userInteractionEnabled = NO; if (text) { self.textLabel.alpha = 0.5f; self.alpha = 0.5f; self.detailTextLabel.hidden = YES; } } } - (void)enableCell:(BOOL)enabled withText:(BOOL)text withDisclosureIndicator:(BOOL)disclosureIndicator { if (enabled) { self.userInteractionEnabled = YES; if (text) { self.textLabel.alpha = 1.0f; self.alpha = 1.0f; self.detailTextLabel.hidden = NO; } self.accessoryType = disclosureIndicator ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; } else { self.userInteractionEnabled = NO; if (text) { self.textLabel.alpha = 0.5f; self.alpha = 0.5f; self.detailTextLabel.hidden = YES; } self.accessoryType = UITableViewCellAccessoryNone; } } - (void)disclosureIndicator:(BOOL)disclosureIndicator { if (disclosureIndicator) { self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } else { self.accessoryType = UITableViewCellAccessoryNone; } } @end 

How to disable a cell:

 [cell enableCell:NO withText:NO]; [cell enableCell:NO withText:YES withDisclosureIndicator:YES]; 

How to enable cell:

 [cell enableCell:YES withText:NO]; [cell enableCell:YES withText:YES withDisclosureIndicator:YES]; 

Hope this helps you.

+2
Apr 15 '14 at 9:39
source share

Swift 4.X

Good continuation from Kevin Owens, I correct the behavior of the cell.

 extension UITableViewCell { func enable(on: Bool) { self.isUserInteractionEnabled = on for view in contentView.subviews { self.isUserInteractionEnabled = on view.alpha = on ? 1 : 0.5 } } } 

How to call it: -

cell.enable(on: switch.isOn)

+2
Jun 30 '18 at 16:35
source share

for quick

 cell.isUserInteractionEnabled = false 
0
May 08 '19 at 9:21
source share



All Articles