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

94
9个回答

175

您可以禁用单元格的文本字段,使它们变灰:

Swift 4.x

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

17
或者更简短一些:cell.userInteractionEnabled = cell.textLabel.enabled = cell.detailTextLabel.enabled = NO; - Thomas Kekeisen
58
虽然更短但更难看 - sports

26

一个适用于我当前场景的Swift扩展;在其他场景中使用效果可能有所不同。

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

现在只需要调用myCell.enable(truthValue)即可。


22

感谢 @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;

当然可以,你也可以通过设置 alpha 值来实现相同的效果 :) - Ajay Sharma

18

尝试使用一个小技巧:

只需设置单元格的不透明度。根据您自己的要求设置一些条件并设置不透明度即可。

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

虽然我不确定这种方式是否正确,但它肯定能够满足您的需求。这将在用户的脑海中产生一种单元格被禁用的错觉。 只需尝试使用此解决方案,希望能解决您的问题。


7
"cell.alpha = 0.2" 对我无效。"cell.ContentView.Alpha = 0.2" 可以正常工作。 - Michal Dobrodenka
同样的问题在这里,设置cell.alpha不起作用,而设置cell.ContentView.alpha就行了。 - infinity_coding7

6

Swift 4.X

来自 Kevin Owens 的不错扩展, 我正在纠正单元格的行为。

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(on: switch.isOn) 方法。

4

这是来自Kevin Owens的一个很棒的扩展,在此我提供一些与 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 3.0中使用.isUserInteractionEnabled。 - Matias Masso

3

Swift 5 版本

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

2

for swift

cell.isUserInteractionEnabled = false

这不会影响单元格的外观。 - Womble

2
我已经创建了一个扩展来启用/禁用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

"UITableViewCell+Ext.m"包含以下内容。

@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];

希望这能对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接