2012-04-17 16 views
15

पर आपको लगता है कि यह आसान होगा। इस कोड के साथ, मैं एक टेबल में कई पंक्तियों की जांच कर सकता हूं लेकिन मैं चाहता हूं कि एक समय में केवल एक पंक्ति की जांच हो। यदि कोई उपयोगकर्ता एक अलग पंक्ति का चयन करता है, तो मैं पुरानी पंक्ति को स्वचालित रूप से अनचेक करना चाहता हूं। यह नहीं पता कि यह कैसे करें। यहां मेरा कोड है:UITableView चेकमार्क केवल एक पंक्ति

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 


UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath]; 

if (theCell.accessoryType == UITableViewCellAccessoryNone) { 
    theCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

    else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    theCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
} 

किसी भी मदद के लिए धन्यवाद जो आप उधार दे सकते हैं!

+1

आपके उन प्रश्नों के लिए जिन्हें सही उत्तर दिया गया था, आपको इसके आगे के चेकमार्क पर क्लिक करके सही उत्तर स्वीकार करना चाहिए। –

उत्तर

22

सबसे अच्छा तरीका है cellForRowAtIndexPath में गौण प्रकार सेट और केवल रिकॉर्ड है जो पथ चयन किया जाना चाहिए करने के लिए didSelectRowAtIndexPath उपयोग करना और फिर reloadData कॉल करने के लिए है।

+1

इस समाधान के साथ एकमात्र समस्या यह है कि आप अचयन पंक्ति एनीमेशन खो देंगे। बस अन्य दृश्यमान कोशिकाओं को फिर से लोड करें। –

+1

मैं निश्चित रूप से पहले सही और बाद में सही चुनता हूं ... यदि आवश्यक हो। :-) –

+1

फिर से धन्यवाद! रीलोड लोड चीजें काम करने की कुंजी थी। आपकी अंतर्दृष्टि बहुत सराहना की। – Jupiter869

17

आप didSelectRowAtIndex पर चयनित इंडेक्स को ट्रैक करने के लिए एक नया चर बना सकते हैं।

int selectedIndex; 

selectedIndex = indexPath.row; 
[tableView reloadData]; 
+0

कृपया – Arun

4

सबसे आसान तरीका अपने cellForRowAtIndexPath

if(indexPath.row == selectedIndex) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

में और अपने didSelectRowAtIndex में चयनित IndexPath बचाने के लिए और cellForRowAtIndexPath में यह जाँच करने के लिए है।

चरण 1:

संलग्न कोड है प्रारंभ selectedIndexPath selectedIndexPath = [[NSIndexPath alloc] init];

चरण 2: cellForRowAtIndexPath में

if([selectedIndexPath isEqual:indexPath]){ 
    [checkMark setHidden:NO]; 
} else { 
    [checkMark setHidden:YES]; 
} 

चरण 3: didSelectRowAtIndexPath

में
selectedIndexPath = indexPath; 
[tableView reloadData]; 

यह काम इस Njoy :)

8

आप tableView फिर से लोड करने की जरूरत नहीं है की कोशिश काम करना चाहिए ।अपने वर्ग के लिए एक NSIndexPath lastSelectedIndexPath चर घोषित

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if(lastSelectedIndexPath) { 

     UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelectedIndexPath]; 
     [lastCell setAccessoryView:nil]; 
    } 

    UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:currentIndexPath]; 
    [currentCell setAccessoryView:UITableViewCellAccessoryCheckmark]; 

    lastSelectedIndexPath = indexPath; 
} 
+0

के बजाय NSINTEGER के रूप में घोषित करें क्या कोई जानता है कि NSUserDeafults का उपयोग करके चयनित पंक्ति की स्थिति को कैसे सहेजना है? मैं ऐसा करने का एक आसान तरीका ढूंढ रहा हूं। मैंने जो भी समाधान पाया है वह काफी विस्तृत है, और मेरे प्रोजेक्ट के लिए जो बस मेरे कोड को अव्यवस्थित कर देगा। –

+0

@ बोर्गेस क्या आप अपने प्रश्न को थोड़ा और समझा सकते हैं? सेल की स्थिति से आपका क्या मतलब है? –

71
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
} 

Swift संस्करण होगा

:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None 
} 

Swift 3अद्यतन

नीचे कोड देखें:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    tableView.cellForRow(at: indexPath)?.accessoryType = .none 
} 
+1

बेस्ट समाधान! धन्यवाद! –

+0

@ थॉमससी। माना! – mikemike396

+2

यह पुन: प्रयोज्यता का ख्याल नहीं रखता है। सावधान। – p0lAris

0
Swift iOS 

    var checkedIndexPath : NSIndexPath? 

    // table row which row was selected 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 

     println("Section #\(indexPath.section)! You selected cell #\(indexPath.row)!") 

     if (self.checkedIndexPath != nil) { 
      var cell = tableView.cellForRowAtIndexPath(self.checkedIndexPath!) 
      cell?.accessoryType = .None 
     } 

     var cell = tableView.cellForRowAtIndexPath(indexPath) 
     cell?.accessoryType = .Checkmark 

     self.checkedIndexPath = indexPath 

    }// end tableView 
+0

यह जब 'checkedIndexPath' tableView में VisibleCells में नहीं है क्रैश हो रहा है अगर (self.checkedIndexPath! = शून्य) { वर सेल = tableView.cellForRowAtIndexPath (self.checkedIndexPath!) सेल? .accessoryType = .None } सेल शून्य है यदि यह दृश्य कक्षों में नहीं है – umakanta

1

इस प्रयास करें:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath.section == 1 { 
     // un-select the older row 
     if let selected = self.LastSelected { 
      tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None 
     } 

     // select the new row 
     tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark 
     self.LastSelected = indexPath 
    } 
} 
10

आप की जरूरत नहीं है (और नहीं होना चाहिए) बस प्रत्येक चयन के बाद तालिका से लोड करें।

ऐप्पल में चयन सूची का प्रबंधन करने के तरीके पर 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 
    } 
} 
+0

यह एक उत्कृष्ट उत्तर है। – damianesteban

+0

यह उत्तर अद्भुत है लेकिन यह मानता है कि 'चयनित इंडेक्सपाथ' प्रारंभ किया गया है, है ना? मेरा मतलब है, यदि तालिका दृश्य प्रारंभ करते समय कोई डिफ़ॉल्ट मान नहीं है, तो 'didEelectRowAtIndexPath' को' चयनित इंडेक्सपैथ 'को खोलने का प्रयास करना चाहिए और मेरे कोड स्निप में मूल्य – matteodv

+0

@matteodv सेट करें, मेरे पास तीसरी पंक्ति पर एक टिप्पणी है जो कहती है' // संभावना है कि आप सेलफॉररोएट इंडेक्सपैथ के दौरान इसे सेट करेंगे जब आप सहेजे गए उपयोगकर्ता चयन या डिफ़ॉल्ट 'से मेल खाने वाले सेल को हटा दें। क्या इससे आपके प्रश्न का उत्तर मिलता है? – jeffjv

0

स्विफ्ट 2.0 में मैं इस प्रयोग किया है::

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    if((lastSelectedIndexPath) != nil) { 
     let lastCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath) 
     lastCell?.accessoryType = UITableViewCellAccessoryType.None 
    } 

    let currentCell = tableView.cellForRowAtIndexPath(indexPath) 
    currentCell?.accessoryType = UITableViewCellAccessoryType.Checkmark 

    lastSelectedIndexPath = indexPath 

} 
+0

आखिरी चयन किए गए इंडेक्सपाथ कैसे प्रारंभ और प्रबंधित किया गया है? आखिरी चयन किए गए इंडेक्सपैथ कभी भी –

+0

var अंतिम चयन किए गए इंडेक्सपैथ: एनएसआईएनएक्सएक्सपाथ नहीं हो सकता है? = NSIndexPath (forRow: 0, inSection: 0) –

+0

'var lastSelectedIndexPath: NSIndexPath! 'शुरूआत में शून्य है, लेकिन बाद में, मैं जांचता हूं कि अगर शून्य है, और फ़ंक्शन के अंत में मैं करता हूं:' lastSelectedIndexPath = indexPath' – Dasoga

1

स्विफ्ट में, निम्न काम करता है पूरी तरह से:

2.0 उदाहरण नीचे तेजी से देखें

lastSelectedIndexPath = NSIndexPath(forRow: 1, inSection: 0)//Row will be the previous selected row 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell:LabelsSelectionCell = tableView.dequeueReusableCellWithIdentifier("LabelsSelectionCell", forIndexPath: indexPath) as! LabelsSelectionCell 
    cell.setCellLael(labelOptionsArray[indexPath.row]) 
    if lastSelectedIndexPath == indexPath 
    { 
     cell.setCellCheckedStatus(true) 
    } 
    return cell 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if let _ = lastSelectedIndexPath 
    { 
     let lastCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath!) as! LabelsSelectionCell 
     lastCell.setCellCheckedStatus(false) 
    } 
    let currentCell:LabelsSelectionCell = tableView.cellForRowAtIndexPath(indexPath) as! LabelsSelectionCell 
    currentCell.setCellCheckedStatus(true) 

    lastSelectedIndexPath = indexPath 

    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 
0

मेरे रास्ते अन्य का चयन रद्द है चयन के दौरान कोशिकाएं:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = .... 

    if condition { 
     cell.accessoryType = .checkmark 
     tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.bottom) 
    } else { 
     cell.accessoryType = .none 
    } 

    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    for row in 0...tableView.numberOfRows(inSection: 0) { 
     if row == indexPath.row {continue} 
     tableView.deselectRow(at: IndexPath(row: row, section: 0), animated: true) 
    } 

    if let cell = tableView.cellForRow(at: indexPath) { 
     cell.accessoryType = .checkmark 
    } 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    cell?.accessoryType = .none 
} 
1

स्विफ्ट 3 के लिए मेरे लिए काम किया है:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    tableVIew.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none 
} 
+0

आदमी काम नहीं करता है।:( – Ashik

+0

कर्कमार्क सेल शैली का उपयोग करने का प्रयास करें – Tarik

0

यहाँ मैं क्या आया जब मैं इस समस्या थी हैं।
यह कोड स्विफ्ट के नवीनतम संस्करण में आज के रूप में लागू किया गया है ...
अधिक जानकारी और/या एक्सटेंशन फ़ाइल के लिए, कृपया इस स्निपेट के Github Gist देखें।

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {  
    if let sip = selectedIndex { 
     tableView.deselectRow(at: sip, animated: false) 
    } 
    selectedIndex = indexPath 
} 

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    if selectedIndex?.row == indexPath.row { 
     selectedIndex = nil 
    } 
} 
0

परीक्षण किया है और हल यह इसकी पूरी तरह से काम किया है की कोशिश

वैश्विक चर के रूप में जोड़

var selectedIndexPath = NSIndexPath(row: 0, section: 0) 

didselect विधि में इस जोड़े

 // old one check off 
     if indexPath != selectedIndexPath as IndexPath { 
      let oldCell = categorytable.cellForRow(at: selectedIndexPath as IndexPath) 
      if oldCell?.accessoryView != nil { 
       oldCell?.accessoryView = nil 
      } 
      else { 
       let imageName = "checkmark" 
       let image: UIImageView = UIImageView(image: UIImage(named: imageName)) 
       cell.accessoryView = image 
      } 
     } 
     // the new one on 

     if cell.accessoryView == nil { 
      let imageName = "checkmark" 
      let image: UIImageView = UIImageView(image: UIImage(named: imageName)) 
      cell.accessoryView = image 
     } 
     else { 
      cell.accessoryView = nil 
     } 
संबंधित मुद्दे