web-dev-qa-db-fra.com

Comment faire apparaître un UITableViewCell désactivé?

Je connais ITableview: comment désactiver la sélection pour certaines lignes mais pas pour d'autres et cell.selectionStyle = UITableViewCellSelectionStyleNone, mais comment faire pour qu'une cellule (ou n'importe quelle UIView d'ailleurs) apparaisse désactivée (grisée) comme ci-dessous?

disabled UITableViewCell

83
ma11hew28

Vous pouvez simplement désactiver les champs de texte de la cellule pour les griser:

Swift 4.x

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

Une extension Swift qui fonctionne bien dans le contexte où je l'utilise; votre kilométrage peut varier.

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

Il ne reste plus qu'à appeler myCell.enable(truthValue).

22
Kevin Owens

Grâce à @Ajay Sharma, j'ai compris comment rendre un UITableViewCellapparaît désactivé:

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

Et puis, pour en fait désactiver la cellule:

cell.userInteractionEnabled = NO;
22
ma11hew28

Essayez d'utiliser une petite astuce:

Réglez simplement l'alpha de la cellule. Mettez une condition comme vos propres exigences et définissez l'alpha.

cell.alpha=0.2;

Si cela ne fonctionne pas, comme vous l'aimez, utilisez la deuxième astuce,

Prenez simplement une image de la taille de la cellule avec un fond gris avec un fond transparent, ajoutez simplement cette image dans l'image sur le contenu de la cellule. Comme ça:

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

Bien que je ne sois pas sûr du chemin, mais cela répondra sûrement à vos exigences. Cela donnera une sorte d'illusion dans l'esprit de l'utilisateur que la cellule est désactivée. Essayez simplement d'utiliser cette solution.J'espère que cela résoudra votre problème.

17
Ajay Sharma

Grande extension de Kevin Owens, c'est ma correction pour travailler avec 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
        }
    }
}
4

J'ai créé l'extension suivante pour activer/désactiver UITableViewCell, il est très pratique de l'utiliser. Créer l'extension UITableViewCell avec "UITableViewCell + Ext.h" contient les suivants.

@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" contient ce qui suit.

@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

Comment désactiver la cellule:

[cell enableCell:NO withText:NO];

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

Comment activer la cellule:

[cell enableCell:YES withText:NO];

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

J'espère que cela vous aide.

2
Aqib Mumtaz

Swift 4.X

Belle extension de Kevin Owens, je corrige le comportement de la cellule.

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

Comment appeler cela: -

cell.enable(on: switch.isOn)

2
Ashu

pour Swift

cell.isUserInteractionEnabled = false
0
Nilesh