आप की जरूरत नहीं है (और नहीं होना चाहिए) बस प्रत्येक चयन के बाद तालिका से लोड करें।
ऐप्पल में चयन सूची का प्रबंधन करने के तरीके पर documentation अच्छा है। उदाहरण के लिए लिस्टिंग 6-3 देखें।
यह कुछ अन्य लोगों के समान उत्तर है, लेकिन मैंने सोचा कि मैं थोड़ा और विस्तार जोड़ूंगा।
आप जो करना चाहते हैं वह वर्तमान चयनित इंडेक्सपैथ को एक चर में सहेजना है और पुराने चेक मार्क को हटाने के लिए didSelectRowAtIndexPath में इसका उपयोग करना है। यह वह जगह भी है जहां आप नया चेक मार्क जोड़ देंगे।
आपको सेलफॉररोएट इंडेक्सपैथ में चेकमार्क को सेट/अनसेट करने की भी आवश्यकता है अन्यथा यदि आपकी सूची बड़ी है और कोशिकाओं का पुन: उपयोग किया जाता है तो यह कई पंक्तियों का चयन किया जाएगा। यह कुछ अन्य उत्तरों के साथ एक समस्या है।
// for saving currently seletcted index path
var selectedIndexPath: NSIndexPath? = NSIndexPath(forRow: 0, inSection: 0) // you wouldn't normally initialize it here, this is just so this code snip works
// likely you would set this during cellForRowAtIndexPath when you dequeue the cell that matches a saved user selection or the default
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// this gets rid of the grey row selection. You can add the delegate didDeselectRowAtIndexPath if you want something to happen on deselection
tableView.deselectRowAtIndexPath(indexPath, animated: true) // animated to true makes the grey fade out, set to false for it to immediately go away
// if they are selecting the same row again, there is nothing to do, just keep it checked
if indexPath == selectedIndexPath {
return
}
// toggle old one off and the new one on
let newCell = tableView.cellForRowAtIndexPath(indexPath)
if newCell?.accessoryType == UITableViewCellAccessoryType.None {
newCell?.accessoryType = UITableViewCellAccessoryType.Checkmark
}
let oldCell = tableView.cellForRowAtIndexPath(selectedIndexPath!)
if oldCell?.accessoryType == UITableViewCellAccessoryType.Checkmark {
oldCell?.accessoryType = UITableViewCellAccessoryType.None
}
selectedIndexPath = indexPath // save the selected index path
// do whatever else you need to do upon a new selection
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
// if this is the currently selected indexPath, set the checkmark, otherwise remove it
if indexPath == selectedIndexPath {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
}
}
आपके उन प्रश्नों के लिए जिन्हें सही उत्तर दिया गया था, आपको इसके आगे के चेकमार्क पर क्लिक करके सही उत्तर स्वीकार करना चाहिए। –