एक UITableView सेल में UISwitch


82

मैं कैसे एक UISwitchपर एम्बेड कर सकते हैंUITableView सेल ? सेटिंग्स मेनू में उदाहरण देखे जा सकते हैं।

मेरा वर्तमान समाधान:

UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;

3
आपके द्वारा किए जा रहे वर्तमान तरीके में क्या गलत है?
MobileMon

जवाबों:


193

इसे एक्सेसरी व्यू के रूप में सेट करना आमतौर पर जाने का तरीका है। आप इसे सेट अप tableView:cellForRowAtIndexPath: कर सकते हैं। स्विच बंद होने पर कुछ करने के लिए आप लक्ष्य / कार्रवाई का उपयोग करना चाह सकते हैं। इस तरह:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch( [indexPath row] ) {
        case MY_SWITCH_CELL: {
            UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
            if( aCell == nil ) {
                aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
                aCell.textLabel.text = @"I Have A Switch";
                aCell.selectionStyle = UITableViewCellSelectionStyleNone;
                UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
                aCell.accessoryView = switchView;
                [switchView setOn:NO animated:NO];
                [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
                [switchView release];
            }
            return aCell;
        }
        break;
    }
    return nil;
}

- (void)switchChanged:(id)sender {
    UISwitch *switchControl = sender;
    NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" );
}

1
MY_SWITCH_CELL के बजाय मेरे विचार से संबंधित सेल नंबर होना चाहिए। समाधान के चारों ओर अच्छा लगा!
परीक्षण

1
@ जेसे 'aCell.accessoryView = switchView?' यह '[aCell setAccessoryView: switchView];' के बराबर है। क्या आपके पास डॉट-नोटेशन से बचने का कोई कारण है?
zpasternack

1
इस उत्तर के लिए बहुत बहुत धन्यवाद! सबव्यू के रूप में स्विच जोड़ने से वॉयस ओवर कमांड में गड़बड़ हो जाती है। गौण दृश्य के साथ एकदम सही काम करता है के रूप में यह सेटिंग!
नितिन अलबुर

2
चुने गए स्विच के सूचकांक को कैसे जानें?
doxsi

2
@doxsi switchView.tag = indexPath.rowपता लगाने के लिए कि कौन सी पंक्ति स्विफ्ट के लिए बदली गई है
Nazmul हसन

10

आप सेल में UISwitch या कोई अन्य नियंत्रण जोड़ सकते हैं accessoryView। इस तरह यह सेल के दायीं ओर दिखाई देगा जो संभवतः आप चाहते हैं।


8
if (indexPath.row == 0) {//If you want UISwitch on particular row
    UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [cell addSubview:theSwitch];
    cell.accessoryView = theSwitch;
}

आप क्यों इस्तेमाल करते हैं initWithFrame? आप क्यों इस्तेमाल करते हैं addSubview? switchएक चर नाम के रूप में इस्तेमाल नहीं किया जा सकता है।
परीक्षण

स्विच नाम के लिए क्षमा करें। मेरा कुछ कोड था .. मैंने इसमें केवल परिवर्तनशील नाम दिया है।
k-थोरैट

इसने मेरे लिए काम किया। कम कोड के साथ प्रभावी समाधान।
केनन काराकासिली

2
मैं केवल सेल की एक्सेसरी व्यू प्रॉपर्टी सेट करके इस काम में सक्षम था। मुझे नहीं लगता कि एक सबव्यू के रूप में स्विच को जोड़ना आवश्यक है।
जॉनीबैन

2

आप इंटरफ़ेसबिल्डर में सेल तैयार कर सकते हैं, इसे अपने व्यूकंट्रोलर के एक IBOutlet से लिंक कर सकते हैं और जब टेबलव्यू उचित पंक्ति के लिए पूछ रहा हो तो इसे वापस कर दें।

इसके बजाय, आप सेल के लिए एक अलग xib बना सकते हैं (फिर से IB के साथ) और इसे UINib का उपयोग करके कोशिकाओं के निर्माण पर लोड कर सकते हैं।

अंत में, आप प्रोग्राम को स्विच बना सकते हैं और इसे अपने सेल कंटेंटव्यू या एक्सेसरीव्यू में जोड़ सकते हैं।

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


मैं प्रोग्रामेटिक सॉल्यूशन (सेटिंग पेज के लिए) के बावजूद इसे पसंद करूंगा, लेकिन मुझे यह भी दिलचस्पी है कि पहले दो विकल्प कैसे काम करते हैं। शायद आप उन्हें और अधिक विस्तार से थोड़ा समझा सकते हैं।
परीक्षण

1

यह एक और पूर्ण समाधान है जहां दृश्य परत (UITableViewCell) पर बंद और चालू होता है और यह आगे की घटनाओं को तालिका दृश्य प्रतिनिधि के माध्यम से didSelectऔर didDeselect:

class CustomCell: UITableViewCell {
    private lazy var switchControl: UISwitch = {
        let s = UISwitch()
        s.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
        return s
    }()

    override func awakeFromNib() {
        self.accessoryView = switchControl
        self.selectionStyle = .none // to show the selection style only on the UISwitch
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        (self.accessoryView as? UISwitch)?.isOn = selected
    }

    @objc private func switchValueDidChange(_ sender: UISwitch) { // needed to treat switch changes as if the cell was selected/unselected
        guard let tv = self.superview as? UITableView, let ip = tv.indexPath(for: self) else {
            fatalError("Unable to cast self.superview as UITableView or get indexPath")
        }
        setSelected(sender.isOn, animated: true)
        if sender.isOn {
            tv.delegate?.tableView?(tv, didSelectRowAt: ip)
        } else {
            tv.delegate?.tableView?(tv, didDeselectRowAt: ip)
        }
    }
}

और अपने डेलीगेट पर


func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    return false // to disable interaction since it happens on the switch
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // to make sure it is rendered correctly when dequeuing:
    // stuff
    if isSelected { // stored value to know if the switch is on or off
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    } else {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    // more stuff
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // do your thing when selecting
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    // do your thing when deselecting
}

0

तेज उपयोगकर्ताओं के लिए

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "TableIdentifer")
        let switch = UISwitch()
        cell.accessoryView = switch 
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.