How do I make a UITableViewCell appear disabled?

2019-03-12 00:17发布

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?

disabled UITableViewCell

7条回答
做自己的国王
2楼-- · 2019-03-12 00:40

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

查看更多
ら.Afraid
3楼-- · 2019-03-12 00:43

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
查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-12 00:46

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
        }
    }
}
查看更多
ら.Afraid
5楼-- · 2019-03-12 00:53

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.

查看更多
乱世女痞
6楼-- · 2019-03-12 00:57

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;
查看更多
forever°为你锁心
7楼-- · 2019-03-12 01:01

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.

查看更多
登录 后发表回答