如何禁用 UITableViewCell?

我知道关于 UITableview: 如何禁用某些行的选择而不禁用其他行的选择cell.selectionStyle = UITableViewCellSelectionStyleNone,但是我如何使一个细胞(或任何 UIView为此事)出现禁用(灰色)像下面?

disabled UITableViewCell

43004 次浏览

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.

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;

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

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.

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).

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

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)

for swift

cell.isUserInteractionEnabled = false

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

}