मैं कैसे एक UISwitch
पर एम्बेड कर सकते हैंUITableView
सेल ? सेटिंग्स मेनू में उदाहरण देखे जा सकते हैं।
मेरा वर्तमान समाधान:
UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
जवाबों:
इसे एक्सेसरी व्यू के रूप में सेट करना आमतौर पर जाने का तरीका है। आप इसे सेट अप 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" );
}
switchView.tag = indexPath.row
पता लगाने के लिए कि कौन सी पंक्ति स्विफ्ट के लिए बदली गई है
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
एक चर नाम के रूप में इस्तेमाल नहीं किया जा सकता है।
आप इंटरफ़ेसबिल्डर में सेल तैयार कर सकते हैं, इसे अपने व्यूकंट्रोलर के एक IBOutlet से लिंक कर सकते हैं और जब टेबलव्यू उचित पंक्ति के लिए पूछ रहा हो तो इसे वापस कर दें।
इसके बजाय, आप सेल के लिए एक अलग xib बना सकते हैं (फिर से IB के साथ) और इसे UINib का उपयोग करके कोशिकाओं के निर्माण पर लोड कर सकते हैं।
अंत में, आप प्रोग्राम को स्विच बना सकते हैं और इसे अपने सेल कंटेंटव्यू या एक्सेसरीव्यू में जोड़ सकते हैं।
आपको कौन सा सूट करना सबसे ज्यादा निर्भर करता है यदि आपकी तालिकाएँ सामग्री निश्चित है (सेटिंग्स पृष्ठ आदि के लिए) तो पहले दो अच्छी तरह से काम कर सकते हैं, यदि सामग्री गतिशील है तो मैं प्रोग्रामेटिक समाधान पसंद करूँगा। कृपया आप क्या करना चाहते हैं, इस बारे में अधिक विशिष्ट होना चाहिए, इससे आपके प्रश्न का उत्तर आसान हो जाएगा।
यह एक और पूर्ण समाधान है जहां दृश्य परत (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
}