如何使UITableViewCell显得已禁用?


Answers:


164

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

斯威夫特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

51
矮小但较笨重
体育运动

25

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

斯威夫特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

尝试使用一个小技巧:

只需设置单元格的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;
}

尽管我不确定这种方式,但这肯定会满足您的要求。这会在用户的脑海中给人一种错觉,认为该单元是Disable。只需尝试使用此解决方案,希望可以解决您的问题。


5
cell.alpha = 0.2-对我不起作用。cell.ContentView.Alpha = 0.2对我
有用

同样在这里设置cell.alpha-不起作用,设置cell.ContentView.alpha可以完成工作
infinity_coding7

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

斯威夫特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,

4

斯威夫特4.X

Kevin Owens的Nice Extension,我正在纠正单元格的行为。

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)


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

希望对您有帮助。


1

迅速

cell.isUserInteractionEnabled = false

这不会影响电池的外观。
Womble
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.