Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使UITableViewCell显示为禁用状态?_Uitableview_Appearance - Fatal编程技术网

如何使UITableViewCell显示为禁用状态?

如何使UITableViewCell显示为禁用状态?,uitableview,appearance,Uitableview,Appearance,我知道和cell.selectionStyle=UITableViewCellSelectionStyleNone,但如何使单元格(或任何UIView)显示为禁用(灰显),如下所示 尝试使用一个小技巧: 只需设置单元格的alpha值。根据您自己的要求设置一些条件并设置alpha cell.alpha=0.2; 如果它不起作用,按照你喜欢的方式,使用第二个技巧 只需拍摄一张具有灰色背景和透明背景的单元格大小图像,只需将该图像添加到单元格内容的图像中即可。 像这样: // Customize th

我知道和
cell.selectionStyle=UITableViewCellSelectionStyleNone
,但如何使单元格(或任何
UIView
)显示为禁用(灰显),如下所示


尝试使用一个小技巧:

只需设置单元格的alpha值。根据您自己的要求设置一些条件并设置alpha

cell.alpha=0.2;
如果它不起作用,按照你喜欢的方式,使用第二个技巧

只需拍摄一张具有灰色背景和透明背景的单元格大小图像,只需将该图像添加到单元格内容的图像中即可。 像这样:

// 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;
}
虽然我不确定该怎么做,但这肯定会满足您的要求。这会在用户的脑海中产生一种错觉,即该单元是禁用的。
请尝试使用此解决方案。希望这能解决您的问题。

多亏了@Ajay Sharma,我想出了如何使
UITableViewCell
显示为禁用状态:

// 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];
然后,要实际禁用该单元:

cell.userInteractionEnabled = NO;

您只需禁用单元格的文本字段即可将其灰显:

Swift 4.x

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

我创建了以下扩展来启用/禁用UITableViewCell,使用起来非常方便。 创建带有“UITableViewCell+Ext.h”的UITableViewCell扩展,其中包含以下内容。

@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
@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
“UITableViewCell+Ext.m”包含以下内容。

@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
@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
如何禁用单元格:

[cell enableCell:NO withText:NO];

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

[cell enableCell:YES withText:YES withDisclosureIndicator:YES];
extension UITableViewCell {
    func enable(on: Bool) {
        for view in contentView.subviews {
            view.isUserInteractionEnabled = on
            view.alpha = on ? 1 : 0.5
        }
    }
}
如何启用单元格:

[cell enableCell:NO withText:NO];

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

[cell enableCell:YES withText:YES withDisclosureIndicator:YES];
extension UITableViewCell {
    func enable(on: Bool) {
        for view in contentView.subviews {
            view.isUserInteractionEnabled = on
            view.alpha = on ? 1 : 0.5
        }
    }
}

希望它能对您有所帮助。

这是一个快速扩展,在我使用它的环境中运行良好;您的里程可能会有所不同

Swift 2.x

cell!.isUserInteractionEnabled = false
cell!.textLabel!.isEnabled = false
cell!.detailTextLabel!.isEnabled = false
extension UITableViewCell {
    func enable(on: Bool) {
        for view in contentView.subviews as! [UIView] {
            view.userInteractionEnabled = on
            view.alpha = on ? 1 : 0.5
        }
    }
}
Swift 3:

[cell enableCell:NO withText:NO];

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

[cell enableCell:YES withText:YES withDisclosureIndicator:YES];
extension UITableViewCell {
    func enable(on: Bool) {
        for view in contentView.subviews {
            view.isUserInteractionEnabled = on
            view.alpha = on ? 1 : 0.5
        }
    }
}

现在只需调用
myCell.enable(truthValue)
凯文·欧文斯的伟大扩展,这是我对使用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:
Swift 4.X

凯文·欧文斯的精彩续集, 我正在纠正细胞的行为

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

cell.enable(打开:switch.isOn)
用于swift

cell.isUserInteractionEnabled = false

当然可以,您也可以这样做,通过设置alpha:)或稍短一点:
cell.userInteractionEnabled=cell.textlab.enabled=cell.detailtextlab.enabled=NO较短,但ugglier thoughcell.alpha=0.2-不适合我。cell.ContentView.Alpha=0.2适用于我这里设置cell.Alpha-不起作用,设置cell.ContentView.Alpha会执行作业使用。在Swift 3.0中,isUserInteractionEnabled这不会影响单元格的外观。