मैं UITableViewCell को कैसे अक्षम कर सकता हूं?


86

मैं UITableview के बारे में जानता हूं: कुछ पंक्तियों के लिए चयन को अक्षम कैसे करेंcell.selectionStyle = UITableViewCellSelectionStyleNone , लेकिन दूसरों को नहीं और , लेकिन मैं एक सेल कैसे बना सकता हूं (या UIViewउस मामले के लिए कोई भी ) नीचे की तरह अक्षम (ग्रे-आउट) दिखाई देता है?

अक्षम UITableViewCell

जवाबों:


164

आप सेल के पाठ फ़ील्ड को उन्हें ग्रे करने के लिए अक्षम कर सकते हैं:

स्विफ्ट 4.x

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

17
या थोड़ा छोटा:cell.userInteractionEnabled = cell.textLabel.enabled = cell.detailTextLabel.enabled = NO;
थॉमस केकेसेन

51
हालांकि छोटी
खेल

25

एक स्विफ्ट एक्सटेंशन जो उस संदर्भ में अच्छी तरह से काम करता है जिसका मैं उपयोग कर रहा हूं; आपकी माइलेज भिन्न हो सकती है।

स्विफ्ट 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 शर्मा के लिए धन्यवाद, मैं एक बनाने के लिए कैसे पता लगा 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;

हाँ, आप इस तरह से भी अल्फा को सेट करके कर सकते हैं :)
अजय शर्मा

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

हालाँकि मैं रास्ते के बारे में निश्चित नहीं हूँ, लेकिन यह निश्चित रूप से आपकी आवश्यकता को पूरा करेगा। यह उपयोगकर्ता के मन में एक प्रकार का भ्रम देगा कि सेल अक्षम है। बस इस समाधान का उपयोग करने का प्रयास करें। जो आपकी समस्या का समाधान करेगा।


5
cell.alpha = 0.2 - मेरे लिए काम नहीं करता है। cell.ContentView.Alpha = 0.2 ने मेरे लिए काम किया
Michal Dobrodenka

यहाँ वही है जो cell.alpha की स्थापना करता है - काम नहीं करता, cell.ContentView.alpha काम करता है
infinity_coding7

4

केविन ओवेन्स से महान विस्तार, यह स्विफ्ट 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
        }
    }
}

उपयोग .isUserInteractionEnabled में स्विफ्ट 3.0
मासो

4

स्विफ्ट 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(on: switch.isOn)


2

मैंने Enable / Disable 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
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.