How do I make a UITableViewCell appear disabled?

UitableviewAppearance

Uitableview Problem Overview


I know about https://stackoverflow.com/questions/2267993/uitableview-how-to-disable-selection-for-some-rows-but-not-others and cell.selectionStyle = UITableViewCellSelectionStyleNone, but how do I make a cell (or any UIView for that matter) appear disabled (grayed-out) like below?

disabled UITableViewCell

Uitableview Solutions


Solution 1 - Uitableview

You can just disable the cell's text fields to gray them out:

Swift 4.x

cell!.isUserInteractionEnabled = false
cell!.textLabel!.isEnabled = false
cell!.detailTextLabel!.isEnabled = false

Solution 2 - Uitableview

A Swift extension that works well in the context I'm using 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 it's just a matter of calling myCell.enable(truthValue).

Solution 3 - Uitableview

Thanks to @Ajay Sharma, I figured out how to make a UITableViewCell appear disabled:

// Mac's 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;

Solution 4 - Uitableview

Try using a small trick:

Just set the alpha of the cell. Put some condition as your own requirements & set the alpha.

cell.alpha=0.2;

If it does't work,the way you like it to be then, Use second trick,

Just take an image of the cell size having gray background with Transparent Background, just add that image in image over the cell content. 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 am not not sure about the way,but this will surely fulfill your requirement.This will give a kind of illusion in user's mind that the cell is Disable. Just try using this solution.Hope that will solve your problem.

Solution 5 - Uitableview

Swift 4.X

Nice Extension from Kevin Owens, I am correcting the behaviour of 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 this:-

cell.enable(on: switch.isOn)

Solution 6 - Uitableview

Great extension from Kevin Owens, this is my correction to 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
        }
    }
}

Solution 7 - Uitableview

I have created following extension to Enable/Disable UITableViewCell, it is very convenient to use it. Create UITableViewCell Extension with "UITableViewCell+Ext.h" contain following in it.

@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" contain following in it.

@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 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 it helps you.

Solution 8 - Uitableview

for swift

cell.isUserInteractionEnabled = false

Solution 9 - Uitableview

Swift 5 version

class CustomCell: UITableViewCell {
    
    private func isEnabled(_ enabled: Bool) {
        isUserInteractionEnabled = enabled
        subviews.forEach { subview in
            subview.isUserInteractionEnabled = enabled
            subview.alpha = enabled ? 1 : 0.5
        }
    }
    
}
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionma11hew28View Question on Stackoverflow
Solution 1 - UitableviewSymmetricView Answer on Stackoverflow
Solution 2 - UitableviewKevin OwensView Answer on Stackoverflow
Solution 3 - Uitableviewma11hew28View Answer on Stackoverflow
Solution 4 - UitableviewAjay SharmaView Answer on Stackoverflow
Solution 5 - UitableviewAshuView Answer on Stackoverflow
Solution 6 - UitableviewAlessandro OrnanoView Answer on Stackoverflow
Solution 7 - UitableviewAqib MumtazView Answer on Stackoverflow
Solution 8 - UitableviewNileshView Answer on Stackoverflow
Solution 9 - UitableviewDenis RybkinView Answer on Stackoverflow