How do I make a UITableViewCell appear disabled?

2019-03-12 00:48发布

问题:

I know about 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?

回答1:

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


回答2:

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;


回答3:

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



回答4:

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.



回答5:

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


回答6:

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.



回答7:

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)