2014-05-18 12 views
15

में चयनित पंक्ति में मेरे पास UITableView है जो पंक्ति का चयन करते समय चेकमार्क प्रदर्शित करता है। समस्या यह है कि जब मैं didSelectRowAtIndexPath में एक पंक्ति का चयन करता हूं और चयनित पंक्ति पर एक चेकमार्क जोड़ता हूं तो यह एक अतिरिक्त चेकमार्क जोड़ता है। यहां मेरा कोडएकाधिक चेकमार्क जब UITableView IOS

कोई भी सहायता बहुत सराहना की जाएगी। अपने .m फ़ाइल में

प्रचार:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    // Configure the cell... 

    cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName]; 

    cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage]; 

    //cell.detailTextLabel.text [email protected]"Breve Descripción de la categoria"; 

    return cell; 

} 

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

    if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { 

[self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone; 

[self.cellSelected removeObject:indexPath]; 

    }else { 

    [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark; 

     [self.cellSelected addObject:indexPath]; 

    } 

    [self checkMark]; 

    [tableView reloadData]; 
} 

- (void)checkMark{ 

    for (NSIndexPath * indexPath in self.cellSelected) { 

     [self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark; 

    } 


} 

उत्तर

41

[self.tableView cellForRowAtIndexPath:indexPath]didSelectRowAtIndexPath में कॉल सटीक सेल वापस नहीं करेगा। यह एक ही सेल, नया सेल या पुन: उपयोग सेल हो सकता है। यदि यह अपने एक्सेसरी व्यू पर एक पुन: उपयोग किया गया सेल है तो एक चेकमार्क है, तो आप चेकमार्क के साथ दो कक्षों को समाप्त कर देंगे।

सरणी में स्टोर करना और इसके अनुसार इसका उपयोग करना बेहतर है। यदि आप एकाधिक चयन करने की योजना बना रहे हैं, तो नीचे दिए गए कोड उदाहरण का उपयोग करें।

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

// Do any additional setup after loading the view, typically from a nib. 
    self.cellSelected = [NSMutableArray array]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Cell Initialisation here 

    if ([self.cellSelected containsObject:indexPath]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } 
    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    //if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below 
    //self.selectedIndexPath = indexPath; 

    //the below code will allow multiple selection 
    if ([self.cellSelected containsObject:indexPath]) 
    { 
     [self.cellSelected removeObject:indexPath]; 
    } 
    else 
    { 
     [self.cellSelected addObject:indexPath]; 
    } 
    [tableView reloadData]; 
} 
+0

क्यों है [tableView deselectRowAtIndexPath: indexPath एनिमेटेड: हाँ] आवश्यक है ?? जब मैं इस पंक्ति पर टिप्पणी करता हूं तो यह भी अच्छी तरह से काम करता है। – Klinkert0728

+0

मुझे एक ही समस्या है, लेकिन एक छवि के साथ, मेरे पास प्रत्येक सेल के लिए एक छवि है और जब मैं एक पंक्ति का चयन करता हूं तो मुझे चयनित पंक्ति की छवि को बदलने की आवश्यकता होती है, लेकिन जब मैं इस छवि को बदलता हूं, तो एक और छवि परिवर्तन भी हो सकता है जब आप सेल को स्पर्श करते हैं तो आप हाइलाइट किए गए व्यवहार को हटाने के लिए – Klinkert0728

+0

अचयनित कर सकते हैं। –

4

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

@interface MyViewController() { 
     NSIndexPath *__selectedPath; 
    } 

tableView:cellForRowAtIndexPath: जांच में अगर दिया indexPath ही है इवर में संग्रहीत के रूप में:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    //configure cell 

    if ([__selectedPath isEqual:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

में tableView:didSelectRowAtIndexPath स्टोर पॉइंटर चयनित NSIndexPath या शून्य अगर सेल desel किया गया है ect

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

    if (cell.accessoryType == UITableViewCellAccessoryNone) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     __selectedPath = indexPath; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     __selectedPath = nil; 
    } 
} 

मैं Xcode में जाँच के बिना यह लिखा है, तो वहाँ कुछ लेखन-त्रुटियों हो सकता है, लेकिन मैं मुख्य विचार दिखाते हैं। मेरी राय में आपको [self checkMark]; पर अपने tableView:cellForRowAtIndexPath: विधि पर कॉल नहीं करना चाहिए।

इसके अलावा यदि आप एक समय में केवल एक चयनित सेल चाहते हैं, तो आपको NSIndexPath स्टोर करने के लिए NSMutableArray नहीं बनाना चाहिए। ऐसा लगता है कि आपके cellSelected एक समय में 2 NSIndexPaths स्टोर करता है, कि आपके पास यह अजीब व्यवहार क्यों है।

+0

आपको बहुत बहुत धन्यवाद, लेकिन मैं क्योंकि मैं indexPath पर निर्भर करते हुए सेल में चुना गया था के साथ एक MutableArray है विधि सही का निशान कॉल करने के लिए की जरूरत है, और मैं जाँच चिह्न के साथ उन कोशिकाओं की जरूरत है। मैं इस आशा से नया हूं कि आप मेरी मदद कर सकते हैं। – Klinkert0728

+0

यदि आप चाहते हैं कि एक बार में केवल एक सेल चुना गया हो, तो NSMutableArray का उपयोग न करें। बस NSIndexPath। एनएसएमयूटेबलएरे मेमोरी में ज्यादा जगह लेता है, इसलिए इसे यहां इस्तेमाल करने की आवश्यकता नहीं है। इसके अलावा जब आप NSIndexPath का उपयोग करते हैं तो आप सुनिश्चित होंगे कि NSIndexPath को केवल एक पॉइंटर संग्रहीत किया जाएगा, और आपकी तालिका के दौरान डेटा पुनः लोड करना देखें, केवल एक या शून्य UITableViewCell का चयन किया जाएगा। – Neru

8

बस didSelectRowAtIndexPath और didDeselectRowAtIndexPath तरीकों और अपने तालिका दृश्य नियंत्रक उदाहरण का उपयोग करें।

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

    [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; 
} 

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

    [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone]; 
} 
+2

जहां तक ​​मुझे लगता है, यह विधि तब तक काम करेगी जब तक सेल का पुन: उपयोग नहीं किया जाता है। जब सेल का पुन: उपयोग किया जाएगा, तो सब कुछ गड़बड़ हो जाएगा। – Vive

संबंधित मुद्दे